Single Number II *

Given an array of integers, every element appears three times except for one. Find that single one.

 

public class Solution {
    public int singleNumber(int[] A) {
        int size = A.length;
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<size;i++) {
            if(map.containsKey(A[i])) {
                int tmp = map.get(A[i]);
                map.put(A[i],tmp+1);
            }
            else {
                map.put(A[i],1);
            }
        }
        Set<Integer> keys = map.keySet();
        for(Integer key:keys) {
            if(map.get(key)==1) return key;
        }
        return 0;
    }
}

 

posted @ 2015-03-22 11:24  mrpod2g  阅读(90)  评论(0编辑  收藏  举报