Skip to content

Commit 673c010

Browse files
committed
solve problem Number Complement
1 parent ad2321f commit 673c010

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ All solutions will be accepted!
66
|:-|:-|:-|:-|
77
|771|[Jewels and Stones](https://leetcode-cn.com/problems/jewels-and-stones/description/)|[java/py/js](./algorithms/JewelsAndStones)|Easy|
88
|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|

algorithms/NumberComplement/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Number Complement
2+
use bitwise to solve this problem
3+
becareful the overflow
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

0 commit comments

Comments
 (0)