提交 7e939c7c 编写于 作者: M Mars Liu

add permutation

上级 be39881b
......@@ -7,6 +7,7 @@
"children": [],
"export": [
"call.json",
"parser.json"
"parser.json",
"permutation.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "permutation.md",
"notebook_enable": false,
"exercise_id": "a52d29ac72cf48669824894ba7f7427a"
}
\ No newline at end of file
# 排列
给定一个几十个字符内的短字符串,简单起见我们假设这个字符串没有重复字符,那么生成其全排列的代码是:
## 答案
```java
package net.csdn;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static String drop(String token, int idx){
if(idx==0){
return token.substring(1);
}
if(idx>=token.length()-1){
return token.substring(0, idx);
}
return token.substring(0, idx) + token.substring(idx+1);
}
public static List<String> permutation(String token){
if(token.length() < 2){
return List.of(token);
}
if(token.length()==2){
String item = token.charAt(1) + String.valueOf(token.charAt(0));
return List.of(token, item);
}
List<String> result = new ArrayList<>();
for(int idx=0;idx<token.length();idx++){
char chr = token.charAt(idx);
String next = drop(token, idx);
for(var rest: permutation(next)){
result.add(chr +rest);
}
}
return result;
}
public static void main(String[] args) {
String token = args[0];
for(var item: permutation(token)){
System.out.println(item);
}
}
}
```
## 选项
### 方法定义不对
```java
package net.csdn;
import java.util.ArrayList;
import java.util.List;
public class Main {
public String drop(String token, int idx) {
if (idx == 0) {
return token.substring(1);
}
if (idx >= token.length() - 1) {
return token.substring(0, idx);
}
return token.substring(0, idx) + token.substring(idx + 1);
}
public List<String> permutation(String token) {
if (token.length() < 2) {
return List.of(token);
}
if (token.length() == 2) {
String item = token.charAt(1) + String.valueOf(token.charAt(0));
return List.of(token, item);
}
List<String> result = new ArrayList<>();
for (int idx = 0; idx < token.length(); idx++) {
char chr = token.charAt(idx);
String next = drop(token, idx);
for (var rest : permutation(next)) {
result.add(chr + rest);
}
}
return result;
}
public static void main(String[] args) {
String token = args[0];
for (var item : permutation(token)) {
System.out.println(item);
}
}
}
```
### 方法引用不对
```java
package net.csdn;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static String drop(String token, int idx){
if(idx==0){
return token.substring(1);
}
if(idx>=token.length()-1){
return token.substring(0, idx);
}
return token.substring(0, idx) + token.substring(idx+1);
}
public static List<String> permutation(String token){
if(token.length() < 2){
return List.of(token);
}
if(token.length()==2){
String item = token.charAt(1) + String.valueOf(token.charAt(0));
return List.of(token, item);
}
List<String> result = new ArrayList<>();
for(int idx=0;idx<token.length();idx++){
char chr = token.charAt(idx);
String next = drop(token, idx);
for(var rest: permutation(next)){
result.add(chr +rest);
}
}
return result;
}
public static void main(String[] args) {
String token = args[0];
for(var item: this.permutation(token)){
System.out.println(item);
}
}
}
```
### 方法定义不对
```java
package net.csdn;
import java.util.ArrayList;
import java.util.List;
public class Main {
String drop(String token, int idx){
if(idx==0){
return token.substring(1);
}
if(idx>=token.length()-1){
return token.substring(0, idx);
}
return token.substring(0, idx) + token.substring(idx+1);
}
List<String> permutation(String token){
if(token.length() < 2){
return List.of(token);
}
if(token.length()==2){
String item = token.charAt(1) + String.valueOf(token.charAt(0));
return List.of(token, item);
}
List<String> result = new ArrayList<>();
for(int idx=0;idx<token.length();idx++){
char chr = token.charAt(idx);
String next = drop(token, idx);
for(var rest: permutation(next)){
result.add(chr +rest);
}
}
return result;
}
public static void main(String[] args) {
String token = args[0];
for(var item: permutation(token)){
System.out.println(item);
}
}
}
```
### D
全都是错的,Java 函数不能递归调用
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册