File tree 5 files changed +40
-1
lines changed
algorithms/NumberComplement
5 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -6,4 +6,5 @@ All solutions will be accepted!
6
6
| :-| :-| :-| :-|
7
7
| 771| [ Jewels and Stones] ( https://leetcode-cn.com/problems/jewels-and-stones/description/ ) | [ java/py/js] ( ./algorithms/JewelsAndStones ) | Easy|
8
8
| 657| [ Judge Route Circle] ( https://leetcode-cn.com/problems/judge-route-circle/description/ ) | [ java/py/js] ( ./algorithms/JudgeRouteCircle ) | Easy|
9
- | 461| [ Hamming Distance] ( https://leetcode-cn.com/problems/hamming-distance/description/ ) | [ java/py/js] ( ./algorithms/HammingDistance ) | Easy|
9
+ | 461| [ Hamming Distance] ( https://leetcode-cn.com/problems/hamming-distance/description/ ) | [ java/py/js] ( ./algorithms/HammingDistance ) | Easy|
10
+ | 476| [ Number Complement] ( https://leetcode-cn.com/problems/number-complement/description/ ) | [ java/py/js] ( ./algorithms/NumberComplement ) | Easy|
Original file line number Diff line number Diff line change
1
+ # Number Complement
2
+ use bitwise to solve this problem
3
+ becareful the overflow
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findComplement (int num ) {
3
+ int x = 0x01 ;
4
+ while (x <= num && x >= 0 ) {
5
+ num = num ^ x ;
6
+ x = x << 1 ;
7
+ }
8
+ return num ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } num
3
+ * @return {number }
4
+ */
5
+ var findComplement = function ( num ) {
6
+ x = 1
7
+ while ( x <= num && x >= 0 ) {
8
+ num = num ^ x
9
+ x = x << 1
10
+ }
11
+ return num
12
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def findComplement (self , num ):
3
+ """
4
+ :type num: int
5
+ :rtype: int
6
+ """
7
+ # use this v to ^
8
+ x = 1
9
+ while x <= num :
10
+ num = num ^ x
11
+ # move left 1 bit
12
+ x = x << 1
13
+ return num
You can’t perform that action at this time.
0 commit comments