137:只出现一次的数字 II(注释修改)

This commit is contained in:
huangge1199 2021-04-30 10:15:23 +08:00
parent fd5f5a7dd9
commit 589a140b96

View File

@ -50,22 +50,23 @@ public class SingleNumberIi {
//leetcode submit region begin(Prohibit modification and deletion)
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 num : map.keySet()) {
// if (map.get(num) == 1) {
// return num;
// }
// }
// return 0;
int ones = 0, twos = 0;
for(int num : nums){
ones = ones ^ num & ~twos;
twos = twos ^ num & ~ones;
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
return ones;
for (int num : map.keySet()) {
if (map.get(num) == 1) {
return num;
}
}
return 0;
// // 官方
// int ones = 0, twos = 0;
// for(int num : nums){
// ones = ones ^ num & ~twos;
// twos = twos ^ num & ~ones;
// }
// return ones;
}
}
//leetcode submit region end(Prohibit modification and deletion)