输出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)); } } }
期望与各位一起交流学习!
相关文章推荐
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 面试题:输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻
- 122345这6个数字,输出它所有可能的组合;要求4不能在第3位,3和5不能相连。
- 输出{1,2,2,3,4,5}排列组合,4不能在第三位,3和5不能相邻
- 针对如"123456"之类的任意字符序列,输出它们所有的排列组合
- (原创)java 输出排列组合,’4‘ 不能在第三位,‘3’,‘5’不能相连。
- 相同数不能相邻的排列问题 组合数学-容斥原理
- 输出n对括号的所有有效排列组合
- 输出4个整数(不重复)的所有排列组合
- 输出字符串的所有排列组合情况
- 任意数字、字符序列,输出它们所有的排列组合
- JAVA 输出指定字符串所有排列组合
- 任意数字序列“123456”之类,输出它们所有的排列组合
- 输出字符串的所有排列组合
- 给定一个字符串,输出所有的排列组合方式
- 有1,2,2,3,4,5六个数,要求输出他们组合的全排列,4不能在第三位,5和3不能相邻