Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problem: No.0476 #2759

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 15 additions & 68 deletions solution/0400-0499/0476.Number Complement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,30 @@

## 解法

### 方法一
### 方法一:位运算

根据题目描述,我们可以通过异或运算来实现取反的操作,步骤如下:

我们首先找到 $\text{num}$ 的二进制表示中最高位的 $1$,位置记为 $k$。

然后,构造一个二进制数,第 $k$ 位为 $0$,其余低位为 $1$,即 $2^k - 1$;

最后,将 $\text{num}$ 与上述构造的二进制数进行异或运算,即可得到答案。

时间复杂度 $O(\log \text{num})$,其中 $\text{num}$ 为输入的整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def findComplement(self, num: int) -> int:
ans = 0
find = False
for i in range(30, -1, -1):
b = num & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
return ans
return num ^ ((1 << num.bit_length()) - 1)
```

```java
class Solution {
public int findComplement(int num) {
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = num & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
}
return ans;
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
}
}
```
Expand All @@ -94,59 +83,17 @@ class Solution {
class Solution {
public:
int findComplement(int num) {
int full = pow(2, int(log2(num)) + 1) - 1;
return full ^ num;
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
}
};
```
```go
func findComplement(num int) int {
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := num & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
}
return ans
return num ^ ((1 << bits.Len(uint(num))) - 1)
}
```

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```python
class Solution:
def findComplement(self, num: int) -> int:
return num ^ (2 ** (len(bin(num)[2:])) - 1)
```

```cpp
class Solution {
public:
int findComplement(int num) {
int ans = 0;
bool find = false;
for (int i = 30; i >= 0; --i) {
int b = num & (1 << i);
if (!find && b == 0) continue;
find = true;
if (b == 0) ans |= (1 << i);
}
return ans;
}
};
```
```ts
function findComplement(num: number): number {
return num ^ (2 ** num.toString(2).length - 1);
Expand Down
83 changes: 15 additions & 68 deletions solution/0400-0499/0476.Number Complement/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,30 @@

## Solutions

### Solution 1
### Solution 1: Bit Manipulation

According to the problem description, we can use XOR operation to implement the flipping operation, the steps are as follows:

First, we find the highest bit of $1$ in the binary representation of $\text{num}$, and the position is denoted as $k$.

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$;

Finally, we perform XOR operation on $\text{num}$ and the constructed binary number to get the answer.

The time complexity is $O(\log \text{num})$, where $\text{num}$ is the input integer. The space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def findComplement(self, num: int) -> int:
ans = 0
find = False
for i in range(30, -1, -1):
b = num & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
return ans
return num ^ ((1 << num.bit_length()) - 1)
```

```java
class Solution {
public int findComplement(int num) {
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = num & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
}
return ans;
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
}
}
```
Expand All @@ -86,59 +75,17 @@ class Solution {
class Solution {
public:
int findComplement(int num) {
int full = pow(2, int(log2(num)) + 1) - 1;
return full ^ num;
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
}
};
```
```go
func findComplement(num int) int {
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := num & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
}
return ans
return num ^ ((1 << bits.Len(uint(num))) - 1)
}
```

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```python
class Solution:
def findComplement(self, num: int) -> int:
return num ^ (2 ** (len(bin(num)[2:])) - 1)
```

```cpp
class Solution {
public:
int findComplement(int num) {
int ans = 0;
bool find = false;
for (int i = 30; i >= 0; --i) {
int b = num & (1 << i);
if (!find && b == 0) continue;
find = true;
if (b == 0) ans |= (1 << i);
}
return ans;
}
};
```
```ts
function findComplement(num: number): number {
return num ^ (2 ** num.toString(2).length - 1);
Expand Down
3 changes: 1 addition & 2 deletions solution/0400-0499/0476.Number Complement/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution {
public:
int findComplement(int num) {
int full = pow(2, int(log2(num)) + 1) - 1;
return full ^ num;
return num ^ ((1LL << (64 - __builtin_clzll(num))) - 1);
}
};
14 changes: 1 addition & 13 deletions solution/0400-0499/0476.Number Complement/Solution.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
func findComplement(num int) int {
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := num & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
}
return ans
return num ^ ((1 << bits.Len(uint(num))) - 1)
}
14 changes: 1 addition & 13 deletions solution/0400-0499/0476.Number Complement/Solution.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
class Solution {
public int findComplement(int num) {
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = num & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
}
return ans;
return num ^ ((1 << (32 - Integer.numberOfLeadingZeros(num))) - 1);
}
}
11 changes: 1 addition & 10 deletions solution/0400-0499/0476.Number Complement/Solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
class Solution:
def findComplement(self, num: int) -> int:
ans = 0
find = False
for i in range(30, -1, -1):
b = num & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
return ans
return num ^ ((1 << num.bit_length()) - 1)
14 changes: 0 additions & 14 deletions solution/0400-0499/0476.Number Complement/Solution2.cpp

This file was deleted.

3 changes: 0 additions & 3 deletions solution/0400-0499/0476.Number Complement/Solution2.py

This file was deleted.

Loading