強化代碼版:查找字符的出現次數(二)
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
還有有更好的方法的話能夠盡情甩臉啊!!!