Skip to content

Commit f127e2c

Browse files
authored
feat: update solutions to lc problem: No.0476 (doocs#2759)
No.0476.Number Complement
1 parent 5782c1a commit f127e2c

File tree

9 files changed

+34
-191
lines changed

9 files changed

+34
-191
lines changed

solution/0400-0499/0476.Number Complement/README.md

+15-68
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,30 @@
5151

5252
## 解法
5353

54-
### 方法一
54+
### 方法一:位运算
55+
56+
根据题目描述,我们可以通过异或运算来实现取反的操作,步骤如下:
57+
58+
我们首先找到 $\text{num}$ 的二进制表示中最高位的 $1$,位置记为 $k$。
59+
60+
然后,构造一个二进制数,第 $k$ 位为 $0$,其余低位为 $1$,即 $2^k - 1$;
61+
62+
最后,将 $\text{num}$ 与上述构造的二进制数进行异或运算,即可得到答案。
63+
64+
时间复杂度 $O(\log \text{num})$,其中 $\text{num}$ 为输入的整数。空间复杂度 $O(1)$。
5565

5666
<!-- tabs:start -->
5767

5868
```python
5969
class Solution:
6070
def findComplement(self, num: int) -> int:
61-
ans = 0
62-
find = False
63-
for i in range(30, -1, -1):
64-
b = num & (1 << i)
65-
if not find and b == 0:
66-
continue
67-
find = True
68-
if b == 0:
69-
ans |= 1 << i
70-
return ans
71+
return num ^ ((1 << num.bit_length()) - 1)
7172
```
7273

7374
```java
7475
class Solution {
7576
public int findComplement(int num) {
76-
int ans = 0;
77-
boolean find = false;
78-
for (int i = 30; i >= 0; --i) {
79-
int b = num & (1 << i);
80-
if (!find && b == 0) {
81-
continue;
82-
}
83-
find = true;
84-
if (b == 0) {
85-
ans |= (1 << i);
86-
}
87-
}
88-
return ans;
77+
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
8978
}
9079
}
9180
```
@@ -94,59 +83,17 @@ class Solution {
9483
class Solution {
9584
public:
9685
int findComplement(int num) {
97-
int full = pow(2, int(log2(num)) + 1) - 1;
98-
return full ^ num;
86+
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
9987
}
10088
};
10189
```
10290
10391
```go
10492
func findComplement(num int) int {
105-
ans := 0
106-
find := false
107-
for i := 30; i >= 0; i-- {
108-
b := num & (1 << i)
109-
if !find && b == 0 {
110-
continue
111-
}
112-
find = true
113-
if b == 0 {
114-
ans |= (1 << i)
115-
}
116-
}
117-
return ans
93+
return num ^ ((1 << bits.Len(uint(num))) - 1)
11894
}
11995
```
12096

121-
<!-- tabs:end -->
122-
123-
### 方法二
124-
125-
<!-- tabs:start -->
126-
127-
```python
128-
class Solution:
129-
def findComplement(self, num: int) -> int:
130-
return num ^ (2 ** (len(bin(num)[2:])) - 1)
131-
```
132-
133-
```cpp
134-
class Solution {
135-
public:
136-
int findComplement(int num) {
137-
int ans = 0;
138-
bool find = false;
139-
for (int i = 30; i >= 0; --i) {
140-
int b = num & (1 << i);
141-
if (!find && b == 0) continue;
142-
find = true;
143-
if (b == 0) ans |= (1 << i);
144-
}
145-
return ans;
146-
}
147-
};
148-
```
149-
15097
```ts
15198
function findComplement(num: number): number {
15299
return num ^ (2 ** num.toString(2).length - 1);

solution/0400-0499/0476.Number Complement/README_EN.md

+15-68
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,30 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Bit Manipulation
47+
48+
According to the problem description, we can use XOR operation to implement the flipping operation, the steps are as follows:
49+
50+
First, we find the highest bit of $1$ in the binary representation of $\text{num}$, and the position is denoted as $k$.
51+
52+
Then, we construct a binary number, where the $k$-th bit is $0$ and the rest of the lower bits are $1$, which is $2^k - 1$;
53+
54+
Finally, we perform XOR operation on $\text{num}$ and the constructed binary number to get the answer.
55+
56+
The time complexity is $O(\log \text{num})$, where $\text{num}$ is the input integer. The space complexity is $O(1)$.
4757

4858
<!-- tabs:start -->
4959

5060
```python
5161
class Solution:
5262
def findComplement(self, num: int) -> int:
53-
ans = 0
54-
find = False
55-
for i in range(30, -1, -1):
56-
b = num & (1 << i)
57-
if not find and b == 0:
58-
continue
59-
find = True
60-
if b == 0:
61-
ans |= 1 << i
62-
return ans
63+
return num ^ ((1 << num.bit_length()) - 1)
6364
```
6465

6566
```java
6667
class Solution {
6768
public int findComplement(int num) {
68-
int ans = 0;
69-
boolean find = false;
70-
for (int i = 30; i >= 0; --i) {
71-
int b = num & (1 << i);
72-
if (!find && b == 0) {
73-
continue;
74-
}
75-
find = true;
76-
if (b == 0) {
77-
ans |= (1 << i);
78-
}
79-
}
80-
return ans;
69+
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
8170
}
8271
}
8372
```
@@ -86,59 +75,17 @@ class Solution {
8675
class Solution {
8776
public:
8877
int findComplement(int num) {
89-
int full = pow(2, int(log2(num)) + 1) - 1;
90-
return full ^ num;
78+
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
9179
}
9280
};
9381
```
9482
9583
```go
9684
func findComplement(num int) int {
97-
ans := 0
98-
find := false
99-
for i := 30; i >= 0; i-- {
100-
b := num & (1 << i)
101-
if !find && b == 0 {
102-
continue
103-
}
104-
find = true
105-
if b == 0 {
106-
ans |= (1 << i)
107-
}
108-
}
109-
return ans
85+
return num ^ ((1 << bits.Len(uint(num))) - 1)
11086
}
11187
```
11288

113-
<!-- tabs:end -->
114-
115-
### Solution 2
116-
117-
<!-- tabs:start -->
118-
119-
```python
120-
class Solution:
121-
def findComplement(self, num: int) -> int:
122-
return num ^ (2 ** (len(bin(num)[2:])) - 1)
123-
```
124-
125-
```cpp
126-
class Solution {
127-
public:
128-
int findComplement(int num) {
129-
int ans = 0;
130-
bool find = false;
131-
for (int i = 30; i >= 0; --i) {
132-
int b = num & (1 << i);
133-
if (!find && b == 0) continue;
134-
find = true;
135-
if (b == 0) ans |= (1 << i);
136-
}
137-
return ans;
138-
}
139-
};
140-
```
141-
14289
```ts
14390
function findComplement(num: number): number {
14491
return num ^ (2 ** num.toString(2).length - 1);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public:
33
int findComplement(int num) {
4-
int full = pow(2, int(log2(num)) + 1) - 1;
5-
return full ^ num;
4+
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
65
}
76
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
11
func findComplement(num int) int {
2-
ans := 0
3-
find := false
4-
for i := 30; i >= 0; i-- {
5-
b := num & (1 << i)
6-
if !find && b == 0 {
7-
continue
8-
}
9-
find = true
10-
if b == 0 {
11-
ans |= (1 << i)
12-
}
13-
}
14-
return ans
2+
return num ^ ((1 << bits.Len(uint(num))) - 1)
153
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
class Solution {
22
public int findComplement(int num) {
3-
int ans = 0;
4-
boolean find = false;
5-
for (int i = 30; i >= 0; --i) {
6-
int b = num & (1 << i);
7-
if (!find && b == 0) {
8-
continue;
9-
}
10-
find = true;
11-
if (b == 0) {
12-
ans |= (1 << i);
13-
}
14-
}
15-
return ans;
3+
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
164
}
175
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
class Solution:
22
def findComplement(self, num: int) -> int:
3-
ans = 0
4-
find = False
5-
for i in range(30, -1, -1):
6-
b = num & (1 << i)
7-
if not find and b == 0:
8-
continue
9-
find = True
10-
if b == 0:
11-
ans |= 1 << i
12-
return ans
3+
return num ^ ((1 << num.bit_length()) - 1)

solution/0400-0499/0476.Number Complement/Solution2.cpp

-14
This file was deleted.

solution/0400-0499/0476.Number Complement/Solution2.py

-3
This file was deleted.

0 commit comments

Comments
 (0)