File tree 5 files changed +56
-0
lines changed
algorithms/SingleNumberII
5 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ All solutions will be accepted!
39
39
| 404| [ Sum Of Left Leaves] ( https://leetcode-cn.com/problems/sum-of-left-leaves/description/ ) | [ java/py/js] ( ./algorithms/SumOfLeftLeaves ) | Easy|
40
40
| 283| [ Move Zeroes] ( https://leetcode-cn.com/problems/move-zeroes/description/ ) | [ java/py/js] ( ./algorithms/MoveZeroes ) | Easy|
41
41
| 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|
42
43
| 1| [ Two Sum] ( https://leetcode-cn.com/problems/two-sum/description/ ) | [ java/py/js] ( ./algorithms/TwoSum ) | Easy|
43
44
| 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|
44
45
| 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|
Original file line number Diff line number Diff line change
1
+ # Single Number II
2
+ We can solve this problem by bitwise
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments