Array:Given an array of integers
2016-06-12 本文已影响16人
敲一手烂代码
public int singleNumber1(int[] nums) {
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hashMap.containsKey(nums[i])) {
if (hashMap.get(nums[i])==2) {
hashMap.remove(nums[i]);
} else {
hashMap.put(nums[i], hashMap.get(nums[i])+1);
}
} else {
hashMap.put(nums[i], 1);
}
}
for (int i : hashMap.keySet()) {
return i;
}
return -1;
}