137. Single Number II
Given an array of integers, every element appearsthreetimes except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路:用HashSet
public class Solution {
public int singleNumber(int[] nums) {
LinkedHashMap<Integer,Integer> map = new LinkedHashMap<Integer,Integer>();
for (int i=0;i<nums.length;i++){
int a = nums[i];
if (map.containsKey(a)){
int Val = map.get(a);
if (Val==1){
map.put(a,map.get(a)+1);
}else if(Val==2){
map.remove(a);
}
}else{
map.put(a,1);
}
}
Iterator<Integer> it = map.keySet().iterator();
int me = it.next();
return me;
}
}