136. Single Number
use hashmap
T: O(n)
S: O(n)
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0)+1);
}
for (int key : map.keySet()) {
if (map.get(key) == 1) {
return key;
}
}
return -1;
}
}juse bit
一樣的會變 0

T: O(n)
S: O(1)
or
Last updated
Was this helpful?