一个java面试题 采用递归思想编码实现列出1,2,2,3,4这5个数字能表示出的所有的组合数,比如12234,21234等。不能有重复的数。我个人看到这个题目的时候,思考了一会,可是没有想出用什么好的方法来实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) { String str="123"; char[] strArray = str.toCharArray(); permute(strArray, 0, 2); } public static void permute(char[] list, int low, int high) { int i; if (low == high) { String cout = ""; for (i = 0; i <= high; i++) cout += list[i]; System.out.println(cout); } else { for (i = low; i <= high; i++) { char temp = list[low]; list[low] = list[i]; list[i] = temp; permute(list, low + 1, high); temp = list[low]; list[low] = list[i]; list[i] = temp; } } } 改成: String str="12345"; char[] strArray = str.toCharArray(); permute(strArray, 0, 4); 对字符也有效 public class Test{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] p_rs = getResult(5); for(String temp:p_rs){ System.out.println(temp); } } public static String[] getResult(int num){ String[] enums = new String[num]; for (int i = 0; i < num; i++) { enums[i] = Integer.toString(i + 1); } return resultant(num, enums); } private static String[] resultant(int length, String[] ary){ if (length > 1){ String[] tail = resultant(length - 1, ary); String[] rs = new String[tail.length * ary.length]; for (int i = 0; i < ary.length; i++) { for (int j = 0; j < tail.length; j++) { rs[i*tail.length + j] = ary[i] + tail[j]; } } return rs; }else{ return ary; } }} 招计算机专业的实习生,实习三个月,不收取任何费用,提供每天10元的餐补.如有意者,将简历发到[email protected]信箱里. jpcap怎么没有IPAdress类 求1到100前5个能整除3的数,能编译,运行没结果,怎么回事 研究反射机制出现的问题 关于public Scene load(java.net.URL url)出错的问题 JDBC for mysql socket+jdbc 的问题! 怎么把.class文件转变成.java文件?用什么命令? 新手请教 静态的初始化 一個看著簡單不容易解決的問題. DatagramSocket与DatagramPacket问题????? 请问GUI的多线程应该如何设置?
String str="123";
char[] strArray = str.toCharArray();
permute(strArray, 0, 2);
}
public static void permute(char[] list, int low, int high) {
int i;
if (low == high) {
String cout = "";
for (i = 0; i <= high; i++)
cout += list[i];
System.out.println(cout);
} else { for (i = low; i <= high; i++) {
char temp = list[low];
list[low] = list[i];
list[i] = temp;
permute(list, low + 1, high);
temp = list[low];
list[low] = list[i];
list[i] = temp;
}
}
}
char[] strArray = str.toCharArray();
permute(strArray, 0, 4); 对字符也有效
public class Test{ /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] p_rs = getResult(5);
for(String temp:p_rs){
System.out.println(temp);
}
}
public static String[] getResult(int num){
String[] enums = new String[num];
for (int i = 0; i < num; i++) {
enums[i] = Integer.toString(i + 1);
}
return resultant(num, enums);
}
private static String[] resultant(int length, String[] ary){
if (length > 1){
String[] tail = resultant(length - 1, ary);
String[] rs = new String[tail.length * ary.length];
for (int i = 0; i < ary.length; i++) {
for (int j = 0; j < tail.length; j++) {
rs[i*tail.length + j] = ary[i] + tail[j];
}
}
return rs;
}else{
return ary;
}
}
}
如有意者,将简历发到[email protected]信箱里.