137:只出现一次的数字 II(注释修改)
This commit is contained in:
parent
fd5f5a7dd9
commit
589a140b96
@ -50,22 +50,23 @@ public class SingleNumberIi {
|
|||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
class Solution {
|
class Solution {
|
||||||
public int singleNumber(int[] nums) {
|
public int singleNumber(int[] nums) {
|
||||||
// Map<Integer, Integer> map = new HashMap<>();
|
Map<Integer, Integer> map = new HashMap<>();
|
||||||
// for (int num : nums) {
|
for (int num : nums) {
|
||||||
// map.put(num, map.getOrDefault(num, 0) + 1);
|
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;
|
|
||||||
}
|
}
|
||||||
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)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
Loading…
Reference in New Issue
Block a user