锁定老帖子 主题:公司笔试算法题
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (3)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-08
最后修改:2011-03-08
思路:得到全排列,然后对排列进行甄别
public static void main(String[] args) { char buf[]={'1','2','3','4','5','6'}; perm(buf,0,buf.length-1); } public static void perm(char[] buf,int start,int end){ if(start==end){//递归出口 test(new String(buf)); } else{//全排列 for(int i=start;i<=end;i++){ swap(buf, start, i); perm(buf,start+1,end);//后续元素递归全排列 swap(buf, start, i); } } } static void swap(char[]buf,int start,int end){ char temp=buf[start]; buf[start]=buf[end]; buf[end]=temp; } static void test(String str){//2在6前面,解决两个2的重复问题 if (!str.matches("^..4.*$")&&!str.matches("^.*((35)|(53)).*$")&&str.matches("^.*2.*6.*$")) { System.out.println(str.replace('6', '2')); } } |
|
返回顶楼 | |
发表时间:2011-03-08
可以考虑设计一个六进制的数字 进行运算
|
|
返回顶楼 | |
发表时间:2011-03-08
现在小朋友思想这么复杂干啥。10分钟的题目么。
遍历122345到543221之间的整数,剔除不符合的就行了。 |
|
返回顶楼 | |
发表时间:2011-03-08
最后修改:2011-03-08
AllenZhang 写道 现在小朋友思想这么复杂干啥。10分钟的题目么。
遍历122345到543221之间的整数,剔除不符合的就行了。 楼上真汉子! public void test(){ for (Integer i = 122345; i < 543221; i++) { String string = i.toString(); if(!string.matches(".*[06789].*")&&string.matches(".*1.*")&&string.matches(".*2.*2.*") &&string.matches(".*3.*")&&string.matches(".*4.*")&&string.matches(".*5.*") &&!string.matches("^..4.*$")&&!string.matches("^.*((35)|(53)).*$")){ System.out.println(string); } } } |
|
返回顶楼 | |
发表时间:2011-03-08
package com.zhanglu.test;
import java.util.Iterator; import java.util.Vector; public class Test { private static Vector<String> vector = new Vector<String>(); private static final String cost_str_min = "122345"; private static final String cost_str_max = "543221"; private static boolean MatchExpr(String ex) { boolean bFlag = true; int i = 0; for(i = 0;i < ex.length();++i) { if ( ex.charAt(i) != '1' &&ex.charAt(i) != '2' &&ex.charAt(i) != '3' &&ex.charAt(i) != '4' &&ex.charAt(i) != '5') { bFlag = false; } } if ( bFlag ) { int[] nFlag = new int[5];//1 2 3 4 5 数字存在的次记录 for(int k = 0;k < 5;++k) { nFlag[k] = 0; } for(i = 0;i < ex.length();++i) { if ( ex.charAt(i) == '1' ) { ++nFlag[0]; } else if(ex.charAt(i) == '2') { ++nFlag[1]; } else if(ex.charAt(i) == '3') { ++nFlag[2]; } else if(ex.charAt(i) == '4') { ++nFlag[3]; } else if(ex.charAt(i) == '5') { ++nFlag[4]; } } if ( nFlag[0] == 1 && nFlag[1] == 2 && nFlag[2] == 1 && nFlag[3] == 1 && nFlag[4] == 1) { bFlag = true; } else bFlag = false; } if ( bFlag ) { for(i = 0;i < ex.length();++i) { if( (i == 2 && ex.charAt(i) == '4') || ( i+1 < ex.length() && ex.charAt(i) == '3' && ex.charAt(i+1) == '5') || (i+1 < ex.length() && ex.charAt(i) == '5' && ex.charAt(i+1) == '3') ) { bFlag = false; break; } } return bFlag; } return false; } public static void main(String[] args) { long iMax = Long.valueOf(cost_str_max); long iMin = Long.valueOf(cost_str_min); long nTemp = 0; for(long i = iMin;i <= iMax;++i) { nTemp = i; if (MatchExpr(String.valueOf(nTemp))) { if ( !IsFindElement(String.valueOf(nTemp)) ) vector.add(String.valueOf(nTemp)); } } Iterator<String> iter = vector.iterator(); String strTemp = ""; while(iter.hasNext()) { strTemp = iter.next(); System.out.println(strTemp); } } private static boolean IsFindElement(String findElem) { Iterator<String> iter = vector.iterator(); String strTemp = ""; while(iter.hasNext()) { strTemp = iter.next(); if ( strTemp.equals(findElem) ) return true; } return false; } } |
|
返回顶楼 | |
发表时间:2011-03-08
thanq 写道 AllenZhang 写道 现在小朋友思想这么复杂干啥。10分钟的题目么。
遍历122345到543221之间的整数,剔除不符合的就行了。 楼上真汉子! public void test(){ for (Integer i = 122345; i < 543221; i++) { String string = i.toString(); if(!string.matches(".*[06789].*")&&string.matches(".*1.*")&&string.matches(".*2.*2.*") &&string.matches(".*3.*")&&string.matches(".*4.*")&&string.matches(".*5.*") &&!string.matches("^..4.*$")&&!string.matches("^.*((35)|(53)).*$")){ System.out.println(string); } } } 这个简单明了! |
|
返回顶楼 | |
发表时间:2011-03-08
得向前辈们学习
|
|
返回顶楼 | |
发表时间:2011-03-08
AllenZhang 写道 现在小朋友思想这么复杂干啥。10分钟的题目么。
遍历122345到543221之间的整数,剔除不符合的就行了。 呵呵,不走寻常路!! 真的很经典啊! |
|
返回顶楼 | |
发表时间:2011-03-08
wdz567 写道 thanq 写道 AllenZhang 写道 现在小朋友思想这么复杂干啥。10分钟的题目么。
遍历122345到543221之间的整数,剔除不符合的就行了。 楼上真汉子! public void test(){ for (Integer i = 122345; i < 543221; i++) { String string = i.toString(); if(!string.matches(".*[06789].*")&&string.matches(".*1.*")&&string.matches(".*2.*2.*") &&string.matches(".*3.*")&&string.matches(".*4.*")&&string.matches(".*5.*") &&!string.matches("^..4.*$")&&!string.matches("^.*((35)|(53)).*$")){ System.out.println(string); } } } 这个简单明了! 将心比心,面试官看到漏了处理2的全排列算法,至少知道这个人能在10分钟写出个全排列方法。看到这段代码,只会想到它以后就会跑到产品里去了。 |
|
返回顶楼 | |
发表时间:2011-03-09
public static List<StringBuffer> sort(String[] source,int subSize){ List<StringBuffer> resList = new ArrayList<StringBuffer>(); if(subSize > 2) { List<StringBuffer> tmpRes = sort(source,subSize-1); for (StringBuffer sb : tmpRes) { for(int j = 0; j < sb.length() + 1; j++){ if (subSize == 6 && j == 2) continue;//过滤第3位为4的数据 StringBuffer buffer = new StringBuffer(sb).insert(j,source[subSize-1]); if (subSize == 6 && (buffer.indexOf("35") != -1 || buffer.indexOf("53") != -1)) continue;//过滤包括35或53的数据 resList.add(buffer); } } } else { resList.add(new StringBuffer("22"));//过滤22导致的重复数据 } return resList; } public static void main(String[] args) { String[] condition = new String[]{"2","2","1","3","5","4"}; List<StringBuffer> result = sort(condition,condition.length); for (StringBuffer buffer : result) { System.err.println(buffer); } } |
|
返回顶楼 | |