强化代码版:查找字符的出现次数(二)
html
(一)是我本身思考的java
(二)是同窗找到的正确的代码面试
二者思路一致,在代码功底上就体现出来孰优孰劣了,哈哈哈...算法
--------------------------------------------------------------------------------------------------------------------------数组
同窗在群里发了一到笔试题:dom
一个int数组里面有100个随机数,这些数中有重复的,请找出这100个数中每一个不重复的数的出现次数。测试
恩,大概就是这么个意思。。大数据
当时想了想,得出个List<Map<String,int>>这么个解决方法,思想就是从数组第一个元素开始遍历,而后将结果放到Map里面优化
具体操做就是:spa
数据:int[100]={1,2,2,1,2,132,1321,1,23,2,1,2,...}
step 1: new Map<1,1>
step 2: new Map<2,1>
step 3: find(Map.key==2) then (Map.value)++
就是这么个思想啦。。。
而后就实践了下,原本觉得10多分钟就能搞定,没想到前先后后弄了1个小时。。羞。。
1 package cn.edu.bipt.hcol; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 10 public class Founder { 11 12 static int timeKeeper = 1; 13 14 private List<Map<String, Integer>> mFunction(int n[]) { 15 List<Map<String, Integer>> mList = new ArrayList<Map<String, Integer>>(); 16 HashMap<String, Integer> mMap; 17 18 int target = -1; 19 int counter = 1; 20 String targetKey=null , counterKey=null; 21 22 for (int x : n) { 23 if (mList.isEmpty()) { 24 mMap = new HashMap<String, Integer>(); 25 mMap.put("target"+ x, x); 26 mMap.put("counter" + x, counter); 27 mList.add(mMap); 28 } else { 29 boolean flag = false; 30 for (int i = 0;i < mList.size();i++,target = -1 , targetKey = "",counterKey="") { 31 timeKeeper++; 32 mMap = (HashMap<String, Integer>) mList.get(i); 33 Set mSet = mMap.keySet(); 34 Iterator it = mSet.iterator(); 35 while(it.hasNext()){ 36 String temp = (String)it.next(); 37 char c = temp.charAt(0); 38 if(c=='t'){ 39 targetKey = temp; 40 counterKey = (String)it.next(); 41 }else{ 42 counterKey = temp; 43 targetKey = (String)it.next(); 44 } 45 } 46 target = mMap.get(targetKey); 47 counter = mMap.get(counterKey); 48 if(target == x){ 49 ++counter; 50 mMap.put("counter" + target, counter); 51 flag = true; 52 break; 53 } 54 } 55 if(!flag){ 56 counter = 1; 57 mMap = new HashMap<String, Integer>(); 58 mMap.put("target"+ x, x); 59 mMap.put("counter" + x, counter); 60 mList.add(mMap); 61 } 62 } 63 } 64 return mList; 65 } 66 67 public static void main(String[] args) { 68 int test1n[]={1,1,1,1,3,2,2,3,2,2,3}; 69 int test2n[] = new int[1000]; 70 for(int i = 0;i<test2n.length;i++){ 71 test2n[i] = (int)(Math.random()*10); 72 } 73 74 List<Map<String, Integer>> mList = new Founder().mFunction(test2n); 75 76 for(int x : test2n){ 77 System.out.print(x+" "); 78 } 79 System.out.println(); 80 81 for(int i = 0;i < mList.size();i++){ 82 HashMap<String, Integer> mMap = (HashMap<String, Integer>) mList.get(i); 83 Set<String> set = mMap.keySet(); 84 Iterator<String> it = set.iterator(); 85 while(it.hasNext()){ 86 String key = it.next(); 87 int value = mMap.get(key); 88 System.out.print(key+":"+value+" "); 89 } 90 System.out.println(); 91 } 92 System.out.println("timeKeeper"+":"+timeKeeper); 93 } 94 }
哈哈哈哈,没注释!!!
是的,就是没注释,掌握思想就OK啦。。。(实际上是偷懒啊。。不规范啊。。。八嘎)
最后想了下大数据量怎么办。。其实就是这点让我有了记录下这个算法的想法,若是原始数据是1W,10W。。1亿呢。。。WTF
瞬间就想到了cache块哈哈哈,感受很好玩啊,弄个原始数据10%的cache块,用用FIFO..LRU什么的搞搞命中算不算是一种优化呢
有时间再玩啦,话说除了面试题,谁TM还数字符玩呢。。
还有,同窗想到的方法是先排序,在遍历计数,貌似也OK啦,不过是要用到什么二分之类的来作可能比较快,他说他用冒泡。。。笑嘻嘻
MRAK下,安智笔试题是数字符串中的字符,同理同理,当初本身仍是用两个for循环来搞,羞羞。。。
最后最后,测试代码均是天然数,没有测试过负数,应该没多差吧,还有,测试了次10W数据量,弄完貌似用了90+W次遍历,主要是由于Map的KeySet愈来愈长的缘故,加个cache块应该会好不少。
用2for的话OMG,用冒泡的话。。OMG,用二分的话。。不知道了O(nlogn)?WTF?蛋疼的时候再搞
各位看官,转载说明,Thanks
还有有更好的方法的话能够尽情甩脸啊!!!