java 输入一个字符串,打印出该字符串中字符的所有排列
3个回答
展开全部
import java.util.Scanner;
public class Demo001 {
public static void main(String[] args) {
String str = "";
Scanner scan = new Scanner(System.in);
str = scan.nextLine();
permutation(str.toCharArray(), 0);
}
public static void permutation(char[] str, int i) {
if (i >= str.length)
return;
if (i == str.length - 1) {
System.out.println(String.valueOf(str));
} else {
for (int j = i; j < str.length; j++) {
char temp = str[j];
str[j] = str[i];
str[i] = temp;
permutation(str, i + 1);
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
}
}
运行结果:
展开全部
/*
* 前人作品,思想供参考
*/
public class Main{
public static void main(String[] args) {
System.out.println(permutation("abcd").length);
System.out.println(Arrays.toString(permutation("abcd")));
}
private static String[] permutation(String orginal){
ArrayList list = new ArrayList();
if(orginal.length() == 1){
return new String[]{orginal};
} else{
for (int i = 0; i < orginal.length(); i++) {
String s = orginal.charAt(i) + "";
String result = "";
String resultA = result + s;
String leftS = orginal.substring(0, i) + orginal.substring(i + 1, orginal.length());
for (String element : permutation(leftS)) {
result = resultA + element;
list.add(result);
}
}
return (String[]) list.toArray(new String[list.size()]);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-06-22
展开全部
排列组合啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询