输入字符串的java排列
您好,这是我在工作面试中遇到的一个基本问题,我正在尝试使用Java实现输入字符串的所有排列,不幸的是,我无法实现这一点
import java.util.Scanner;
public class Test2 {
static void permute(char[] x, int y){
if (y == x.length)
{
for(int i = 0; i < x.length; i++){
System.out.print(x[y]);
}
}
else {
for (int i = y; i < x.length;i++)
{
char temp = x[y];
x[y] = x[i];
x[i] = temp;
permute(x, y + 1);
temp = x[y];
x[y] = x[i];
x[i] = temp;
}
}
}
public static void main(String [] Args){
Scanner scan = new Scanner (System.in);
System.out.println("Input any word :");
String word = scan.nextLine();
int n = word.length();
char [] sequence = new char[n];
for (int i = 0; i < n ; i++)
sequence[i] = scan.next().charAt(0);
System.out.println("These are the permutations: ");
permute(sequence,0);
}
}
# 1 楼答案
我正在做一个C语言的程序,我希望把它转换成你选择的语言不会太困难
资料来源:-http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
# 2 楼答案