用1、2、2、3、4、5这六个数字,打印出所有不同的排列
2017-07-03 20:46
399 查看
转换成整型值,用累加的方法遍历从最小到最大的所有数值,输出包含指定字符及其指定个数的数字。
由于要遍历更多的数字,所以耗时较大。
递归:
遍历抽取每个字母作为前缀,然后从剩下的字符中抽取字母与其组合。
第二种递归方法,与前面的不同点在于,使用了List代替String来操作数据,并使用LinkedList来提取并移除每个元素,避免了字符串拼接操作。使用StringBuilder存储前面已经出现的字符,避免递归后面重复的字符。
1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.
要求:”4”不能在第三位,”3”与”5”不能相连。
方法一:
根据上道题修改,输入前增加判断
方法三:
由于要遍历更多的数字,所以耗时较大。
public class Main { public static int count = 0; private static String[] mustExistNumber=new String[]{"1","2","2","3","4","5"}; public static void main(String[] args) { String s = "122345"; for(int i=122345;i<=543221;i++){ if(validateNum(i+"")){ System.out.println(i); count++; } } System.out.println(count); } public static boolean validateNum(String num){ for(String number:mustExistNumber){ if(num.indexOf(number)<0){ return false; } if(num.indexOf("2")==num.lastIndexOf("2")){ return false; } } return true; } }
递归:
遍历抽取每个字母作为前缀,然后从剩下的字符中抽取字母与其组合。
public static int count=0; public static void main(String[] args) { String s="122345"; Pailie(s,""); System.out.println(count); } static void Pailie(String s,String p) { if(s.length()<1){ System.out.println(p); count++; }else{ int index[]=new int[s.length()]; for(int i=0;i<s.length();i++){ index[i]=s.indexOf(s.charAt(i)); } for(int i=0;i<s.length();i++){ if(i==index[i]){//保证相同字符中的第一次调用 Pailie(s.substring(1),p+s.substring(0, 1)); } s=s.substring(1)+s.substring(0,1);//循环移位,使每次都是在起始位置选择第一个 } } }
第二种递归方法,与前面的不同点在于,使用了List代替String来操作数据,并使用LinkedList来提取并移除每个元素,避免了字符串拼接操作。使用StringBuilder存储前面已经出现的字符,避免递归后面重复的字符。
public static int count=0; public static void main(String[] args) throws Exception{ long startTime = System.currentTimeMillis(); //获取开始时间 // String[] array=new String[]{"1","2","2","3","4","5"}; // listAll(Arrays.asList(array),""); String s = "122345"; listAll(Arrays.asList(s.split("")),""); System.out.println(count); long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间 } private static void listAll(List<String> candidate, String prefix) { if(candidate.isEmpty()) { System.out.println(prefix); count++; }else { StringBuilder sb=new StringBuilder();//记录前面已经出现的字符 for (int i = 0; i < candidate.size(); i++) { if(sb.indexOf(candidate.get(i))!=-1){//如果是重复字符就不递归 continue; } List temp = new LinkedList(candidate);//为了方便提取后删除每个元素 listAll(temp, prefix + temp.remove(i)); sb.append(candidate.get(i)); } } }
1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.
要求:”4”不能在第三位,”3”与”5”不能相连。
方法一:
private static String[] mustExistNumber = new String[] { "1", "2", "2", "3", "4", "5" }; private static boolean isValidNumber(String str) { // 检查是否包含12345这五个数,不包含返回false for (String number : mustExistNumber) { if (str.indexOf(number) < 0) return false; } // 检查是否有两个2,只有一个返回false if (str.lastIndexOf("2") == str.indexOf("2")) { return false; } // 检查4在不在第三位,是返回false if (str.charAt(2) == '4') { return false; } // 检查是否存在35在一起,有返回false if (str.indexOf("35") >= 0 || str.indexOf("53") >= 0) { return false; } return true; } public static void main(String[] args) { int count=0; for (int i = 122345; i <=543221; i++) { if (isValidNumber(String.valueOf(i))) { System.out.println(i); count++; } } System.out.println(count); }
根据上道题修改,输入前增加判断
public static int count=0; public static void main(String[] args) throws Exception{ long startTime = System.currentTimeMillis(); //获取开始时间 // String[] array=new String[]{"1","2","2","3","4","5"}; // listAll(Arrays.asList(array),""); String s = "122345"; listAll(Arrays.asList(s.split("")),""); System.out.println(count); long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间 } private static void listAll(List<String> candidate, String prefix) { if(candidate.isEmpty()) { if(prefix.charAt(2)=='4'||prefix.contains("35")||prefix.contains("53")){ return; } System.out.println(prefix); count++; }else { StringBuilder sb=new StringBuilder();//记录前面已经出现的字符 for (int i = 0; i < candidate.size(); i++) { if(sb.indexOf(candidate.get(i))!=-1){//如果是重复字符就不递归 continue; } List temp = new LinkedList(candidate);//为了方便提取后删除每个元素 listAll(temp, prefix + temp.remove(i)); sb.append(candidate.get(i)); } } }
方法三:
public static Set<String> set = new TreeSet<String>(); public static void perm(char[] n, int beg, int end) {//每位与最后一位交换,对新串递归 if (beg == end) { addNumber(String.valueOf(n)); } else { for (int i = beg; i <= end; ++i) { swap(n, beg, i); perm(n, beg + 1, end); swap(n, beg, i); } } } public static void swap(char[] n, int x, int y) {//交换 if (x == y || n[x] == n[y]) { return; } char temp = n[x]; n[x] = n[y]; n[y] = temp; } public static void addNumber(String str) {//如果第三位是4,或者3、5相连,不添加到集合中 if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) { return; } set.add(str); } public static void main(String args[]) { char[] number = new char[] { '1', '2', '2', '3', '4', '5' }; perm(number, 0, number.length - 1); System.out.println(set.size()); int cols = 10; for (String s : set) { System.out.print(s + " "); if (cols-- == 1) { System.out.println(); cols = 10; } } }
相关文章推荐
- 面试题─用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列
- 2013年用友校招编程题:用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- Java经典算法集——如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列
- 用1、2、2、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- // 40、用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, // 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"与"5"不能相连.
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
- 该公司笔试题就1个,要求在10分钟内作完。 题目如下:用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列, 如:512234、412325等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,打印出所有不同的排列,……
- 用1、2、2、3、4、5这六个数字,写一个main函数,打印出所有不同的排列
- 用1、2、3、3、4、5这六个数字,用java写一个程序,打印出所有不同的排列 要求:"4"不能在第三位,"3"与"5"不能相连。
- 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。用1、2、2、3、4、5这六
- 用 1,2,2,3,4,5 六个数字,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连