一个软件公司的面试题 把一个数组里的数组合全部列出了 比如1 2 列出来为 1, 2 ,12,21, 尽量简洁. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在不考虑效率,重复的前提下 public static void main(String[] args) throws Exception { String[] array = new String[] { "1", "2", "3", "4" }; listAll(Arrays.asList(array), ""); } public static void listAll(List candidate, String prefix) { if (candidate == null || candidate.isEmpty()) { System.out.println(prefix); } for (int i = 0; i < candidate.size(); i++) { List temp = new LinkedList(candidate); listAll(temp, prefix + temp.remove(i)); } }=====输出=====123412431324134214231432213421432314234124132431312431423214324134123421412341324213423143124321 如果个数不固定,比如lz 1,2 ==> 1,2,12,21 public static void main(String[] args) throws Exception { String[] array = new String[] { "1", "2", "3", "4" }; listAll(Arrays.asList(array), ""); } public static void listAll(List candidate, String prefix) {// if (candidate.isEmpty()) { System.out.println(prefix);// } for (int i = 0; i < candidate.size(); i++) { List temp = new LinkedList(candidate); listAll(temp, prefix + temp.remove(i)); } }=====输出=====1121231234124124313132132413413421414214231431432221213213421421432323123142342341242412413243243133131231243143142323213214324324134341341234234214414124123413413242421421342342314343143124324321 回复人:88324877(看懂请给分,你好我也好。) ( ) 信誉:100 2006-12-29 23:29:04 得分:0? 这里是考数据结构里图的知识吧。====================我认为是树上面用List纯属偷懒,int[]什么也可以,循环的时候,假如当前取出的内容(list.remove(i),相当于get(i)然后remove),在prefix里面,则continue. 先判断数组的元素是否有重复(即相等)?有重复->递归+组合无重复->递归+排列暂时先讨论无重复的情况,数组的元素看作是一个集合,假设为n个,则1): 取一个元素的情况,有n种可能性;2): 取两个元素的情况,则有n*(n-1)/2种可能性,而取得的两个数又可以进行全排列,所以共有2!*n*(n-1)/2=n*(n-1)种可能性;......n-1): 取n-1个元素的情况,有n种可能性,而取得的n-1个数又可以进行全排列,所以共有n*(n-1)!=n!;n): 取n个元素的情况,有1种,一个全排列,共有n!种;所有的可能性为从第1)步到第n)步的可能性之和。编程用递归也不容易,数组元素如果有重复,那就要用到《组合数学》中的定理了。限于时间,不再赘述。 这个考代码的话还是蛮简单的,shine333(enihs) 的回答我觉得挺详细的了,考思想,那就不知道所以然了,进来学习学习 shine333(enihs) 你的程序有我怎么一运行,就报错呀, 我编译了下也报错,刚开始学java,想看看你们的代码listAll.java:1: 'class' or 'interface' expected public static void main(String[] args) throws Exception ^1 error 排列Pn(m)首先去掉相同的数,然后根据排列的公式算出所有的排列,去掉位数2位以上的数的各个位上的数字都在1位数的数字集合里都出现过,最后就是剩下的数组为A,元素个数为N,最后在根据排列公式算出所有的可能!Pn(1)=n!/(n-1)!Pn(2)=n!/(n-2)!Pn(3)=n!/(n-3)!...Pn(n)=n!/(n-n)! (n是数组中有多少个不同的数) 回复人:cooljelly() ( ) 信誉:100 2006-12-30 12:26:56 得分:0? 我编译了下也报错,刚开始学java,想看看你们的代码listAll.java:1: 'class' or 'interface' expectedpublic static void main(String[] args) throws Exception^1 error外面写个classpublic class Test { pubilc static void main .......} 排列Pn(m)首先去掉相同的数, ......=================================大家根本不需要考虑重复不重复这件事情,取的时候,就只有位置信息,而不管他们的数值是多少。最后出来的结果,必然是1121231234124124313......只不过这里1,2的含义不是数值,而是位置信息 shine333 谢谢你呀,能不能加你的QQ呀? 初学I/O问题,求解惑 请教关于工时计算的问题 200分请教,网络传输是不是永远没有USB移动硬盘传递快? Hashmap排序相关问题!谢谢! 接到外企面试通知 紧张 请问高手,,如何学好JSP SERVLET?? 类继承中的问题,请大家帮忙 我的 .class 文件应该放在那里才能运行呀? 菜问题:如何进行数据类型的转化,比如int转化为string <<thinking in java>>中文电子版中没有源码和图表怎么办啊! 请教java中的import java.*,引用类的位置在jdk的哪个目录里面? 如何打包成exe文件
String[] array = new String[] {
"1", "2", "3", "4"
};
listAll(Arrays.asList(array), "");
} public static void listAll(List candidate, String prefix) {
if (candidate == null || candidate.isEmpty()) {
System.out.println(prefix);
}
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i));
}
}=====输出=====
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
String[] array = new String[] {
"1", "2", "3", "4"
};
listAll(Arrays.asList(array), "");
} public static void listAll(List candidate, String prefix) {
// if (candidate.isEmpty()) {
System.out.println(prefix);
// }
for (int i = 0; i < candidate.size(); i++) {
List temp = new LinkedList(candidate);
listAll(temp, prefix + temp.remove(i));
}
}=====输出=====
1
12
123
1234
124
1243
13
132
1324
134
1342
14
142
1423
143
1432
2
21
213
2134
214
2143
23
231
2314
234
2341
24
241
2413
243
2431
3
31
312
3124
314
3142
32
321
3214
324
3241
34
341
3412
342
3421
4
41
412
4123
413
4132
42
421
4213
423
4231
43
431
4312
432
4321
?
这里是考数据结构里图的知识吧。====================
我认为是树上面用List纯属偷懒,int[]什么也可以,循环的时候,假如当前取出的内容(list.remove(i),相当于get(i)然后remove),在prefix里面,则continue.
有重复->递归+组合
无重复->递归+排列暂时先讨论无重复的情况,数组的元素看作是一个集合,假设为n个,则
1): 取一个元素的情况,有n种可能性;
2): 取两个元素的情况,则有n*(n-1)/2种可能性,而取得的两个数又可以进行全排列,所以共有2!*n*(n-1)/2=n*(n-1)种可能性;
......
n-1): 取n-1个元素的情况,有n种可能性,而取得的n-1个数又可以进行全排列,所以共有n*(n-1)!=n!;
n): 取n个元素的情况,有1种,一个全排列,共有n!种;所有的可能性为从第1)步到第n)步的可能性之和。编程用递归也不容易,数组元素如果有重复,那就要用到《组合数学》中的定理了。限于时间,不再赘述。
考思想,那就不知道所以然了,进来学习学习
listAll.java:1: 'class' or 'interface' expected
public static void main(String[] args) throws Exception
^
1 error
最后就是剩下的数组为A,元素个数为N,最后在根据排列公式算出所有的可能!
Pn(1)=n!/(n-1)!
Pn(2)=n!/(n-2)!
Pn(3)=n!/(n-3)!
...
Pn(n)=n!/(n-n)! (n是数组中有多少个不同的数)
? 我编译了下也报错,刚开始学java,想看看你们的代码
listAll.java:1: 'class' or 'interface' expected
public static void main(String[] args) throws Exception
^
1 error外面写个class
public class Test {
pubilc static void main .......
}
1
12
123
1234
124
1243
13
......
只不过这里1,2的含义不是数值,而是位置信息