Skip to content

Commit f56df48

Browse files
committed
solve problem Single Number II
1 parent 429c691 commit f56df48

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ All solutions will be accepted!
3939
|404|[Sum Of Left Leaves](https://leetcode-cn.com/problems/sum-of-left-leaves/description/)|[java/py/js](./algorithms/SumOfLeftLeaves)|Easy|
4040
|283|[Move Zeroes](https://leetcode-cn.com/problems/move-zeroes/description/)|[java/py/js](./algorithms/MoveZeroes)|Easy|
4141
|136|[Single Number](https://leetcode-cn.com/problems/single-number/description/)|[java/py/js](./algorithms/SingleNumber)|Easy|
42+
|137|[Single Number II](https://leetcode-cn.com/problems/single-number-ii/description/)|[java/py/js](./algorithms/SingleNumberII)|Medium|
4243
|1|[Two Sum](https://leetcode-cn.com/problems/two-sum/description/)|[java/py/js](./algorithms/TwoSum)|Easy|
4344
|167|[Two Sum II Input Array Is Sorted](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/description/)|[java/py/js](./algorithms/TwoSumIIInputArrayIsSorted)|Easy|
4445
|653|[Two Sum IV Input Is A Bst](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/)|[java/py/js](./algorithms/TwoSumIVInputIsABst)|Easy|

algorithms/SingleNumberII/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Single Number II
2+
We can solve this problem by bitwise
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int singleNumber(int[] nums) {
3+
int[] bitMap = new int[32];
4+
int res = 0;
5+
6+
for (int i = 0; i < 32; i++) {
7+
for (int num : nums) {
8+
bitMap[i] += num >> i & 1;
9+
}
10+
11+
res |= bitMap[i] % 3 << i;
12+
}
13+
14+
return res;
15+
}
16+
}

algorithms/SingleNumberII/solution.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var singleNumber = function(nums) {
6+
let bitMap = new Array(32),
7+
res = 0
8+
9+
bitMap.fill(0)
10+
11+
for (let i = 0; i < 32; i++) {
12+
nums.forEach(num => {
13+
bitMap[i] += num >> i & 1
14+
})
15+
16+
res |= bitMap[i] % 3 << i
17+
}
18+
19+
return res
20+
};

algorithms/SingleNumberII/solution.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def singleNumber(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
bit_map = [0] * 32
8+
res = 0
9+
10+
for i in range(32):
11+
for num in nums:
12+
bit_map[i] += num >> i & 1
13+
res |= bit_map[i] % 3 << i
14+
# handle the negative in python
15+
if i == 31 and bit_map[i] % 3 == 1:
16+
res -= 0xFFFFFFFF + 1
17+
return res

0 commit comments

Comments
 (0)