java 中,怎么打印出一个字符串的所有排列
1个回答
展开全部
import java.util.Scanner;
public class Test {
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 Test {
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;
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询