83 - 落單的數 II

2017.9.26

就是一個統計的過程吧。用HashMap輕鬆解決吧。

public class Solution {
    /*
     * @param A: An integer array
     * @return: An integer
     */
  	public static int singleNumberII(int[] A) {
        // write your code here
		HashMap<Integer,Integer> map = new HashMap<>();
		for(int i = 0; i < A.length ; i++){
			if(!map.containsKey(A[i])){
				map.put(A[i], 1);
			}
			else{
				map.put(A[i], map.get(A[i]) + 1);
			}
		}
		Iterator itr = map.keySet().iterator();
		while(itr.hasNext()){
			int tmp = (int)itr.next();
			if(map.get(tmp) != 3){
				return tmp;
			}
		}
		return -1;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章