您的位置:首页 > 其它

输出1,2,2,3,4,5的所有排列组合,4不能在第3位,3和5不能相邻

2014-05-05 15:14 806 查看
之前遇到一个面试题,笔试,手写代码。把代码贴出来,大家一起学习交流一下!

import java.util.*;

public class Jtest {
public static void main(String[] args) throws Exception {
test();
System.out.println("一共有:" + map.size() + "个数。");
for (String s:map.values()) {
System.out.println(s);
}
}

private static Map<String, String> map = new HashMap<String, String>();

public static void test() {
String[] strings = { "1", "2", "2", "3", "4", "5" };
// 原始数据
List<String> data = Arrays.asList(strings);
// 第1次循环
for (int i = 0; i < data.size(); i++) {
List<String> temp1 = new ArrayList<String>(data);
// 第1个数
String s1 = temp1.remove(i);
// 第2次循环
for (int j = 0; j < temp1.size(); j++) {
List<String> temp2 = new ArrayList<String>(temp1);
// 第2个数
String s2 = temp2.remove(j);
// 第3次循环
for (int k = 0; k < temp2.size(); k++) {
List<String> temp3 = new ArrayList<String>(temp2);
// 第3个数
String s3 = temp3.remove(k);
// 第4次循环
for (int l = 0; l < temp3.size(); l++) {
List<String> temp4 = new ArrayList<String>(temp3);
// 第4个数
String s4 = temp4.remove(l);
// 第5次循环
for (int m = 0; m < temp4.size(); m++) {
List<String> temp5 = new ArrayList<String>(temp4);
// 第3个数
String s5 = temp5.remove(m);
// 第6次循环
for (int n = 0; n < temp5.size(); n++) {
List<String> temp6 = new ArrayList<String>(
temp5);
// 第3个数
String s6 = temp6.remove(n);
String s = s1 + s2 + s3 + s4 + s5 + s6;
if (s.indexOf("4") != 2
&& s.lastIndexOf("35") == -1
&& s.lastIndexOf("53") == -1) {
map.put(s, s);
}
}
}
}
}
}
}
}
}

感觉代码太臃肿啦,一个排列就搞的这么多,在网上看了一些代码,下面这个同学的代码精简多了,

package com.asher.test;

import java.util.*;

public class Callsort {
public static void main(String[] args) throws Exception {
String[] array = new String[] { "1", "2", "2", "3", "4", "5" };
listAll(Arrays.asList(array), "", array.length);
}

public static void listAll(List<String> candidate, String prefix, int length) {
if (prefix.length() == length && prefix.lastIndexOf("35") == -1
&& prefix.lastIndexOf("53") == -1 && prefix.indexOf("4") != 2)
System.out.println(prefix);
for (int i = 0; i < candidate.size(); i++) {
List<String> temp = new ArrayList<String>(candidate);
listAll(temp, prefix + temp.remove(i), length);
}
}
}

这位同学的没有去掉重复的值。

刚刚想到这个,大家看看,用到递归,思路来之上面的那位同学,精简了一下,去掉重复的值!

import java.util.*;

public class Jtest {
public static void main(String[] args) throws Exception {
String[] array = new String[] { "1", "2", "2", "3", "4", "5" };
group(Arrays.asList(array), "");
System.out.println("一共有:" + map.size() + "个数。");
for (String s : map.values()) {
System.out.println(s);
}
}

private static Map<String, String> map = new HashMap<String, String>();

public static void group(List<String> data, String s) {
if (s.length() == 6 && s.indexOf("4") != 2 && s.lastIndexOf("35") == -1
&& s.lastIndexOf("53") == -1) {
map.put(s, s);
}
for (int i = 0; i < data.size(); i++) {
List<String> temp = new ArrayList<String>(data);
group(temp, s + temp.remove(i));
}
}
}


期望与各位一起交流学习!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: