From 31546b2f6f181985a2a590be3ec2d23dac5ba1c0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 8 May 2024 12:37:33 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.0476 No.0476.Number Complement --- .../0476.Number Complement/README.md | 83 ++++--------------- .../0476.Number Complement/README_EN.md | 83 ++++--------------- .../0476.Number Complement/Solution.cpp | 3 +- .../0476.Number Complement/Solution.go | 14 +--- .../0476.Number Complement/Solution.java | 14 +--- .../0476.Number Complement/Solution.py | 11 +-- .../{Solution2.ts => Solution.ts} | 0 .../0476.Number Complement/Solution2.cpp | 14 ---- .../0476.Number Complement/Solution2.py | 3 - 9 files changed, 34 insertions(+), 191 deletions(-) rename solution/0400-0499/0476.Number Complement/{Solution2.ts => Solution.ts} (100%) delete mode 100644 solution/0400-0499/0476.Number Complement/Solution2.cpp delete mode 100644 solution/0400-0499/0476.Number Complement/Solution2.py diff --git a/solution/0400-0499/0476.Number Complement/README.md b/solution/0400-0499/0476.Number Complement/README.md index fa550b2796af3..188aa89cc7b5e 100644 --- a/solution/0400-0499/0476.Number Complement/README.md +++ b/solution/0400-0499/0476.Number Complement/README.md @@ -51,41 +51,30 @@ ## 解法 -### 方法一 +### 方法一:位运算 + +根据题目描述,我们可以通过异或运算来实现取反的操作,步骤如下: + +我们首先找到 $\text{num}$ 的二进制表示中最高位的 $1$,位置记为 $k$。 + +然后,构造一个二进制数,第 $k$ 位为 $0$,其余低位为 $1$,即 $2^k - 1$; + +最后,将 $\text{num}$ 与上述构造的二进制数进行异或运算,即可得到答案。 + +时间复杂度 $O(\log \text{num})$,其中 $\text{num}$ 为输入的整数。空间复杂度 $O(1)$。 ```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); } } ``` @@ -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) } ``` - - -### 方法二 - - - -```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); diff --git a/solution/0400-0499/0476.Number Complement/README_EN.md b/solution/0400-0499/0476.Number Complement/README_EN.md index 5e2d53ad9fffc..d4052930b5fd7 100644 --- a/solution/0400-0499/0476.Number Complement/README_EN.md +++ b/solution/0400-0499/0476.Number Complement/README_EN.md @@ -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)$. ```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); } } ``` @@ -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) } ``` - - -### Solution 2 - - - -```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); diff --git a/solution/0400-0499/0476.Number Complement/Solution.cpp b/solution/0400-0499/0476.Number Complement/Solution.cpp index fe300aab45d2b..050962653943e 100644 --- a/solution/0400-0499/0476.Number Complement/Solution.cpp +++ b/solution/0400-0499/0476.Number Complement/Solution.cpp @@ -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); } }; \ No newline at end of file diff --git a/solution/0400-0499/0476.Number Complement/Solution.go b/solution/0400-0499/0476.Number Complement/Solution.go index 32f7e84b05d56..a266e55272947 100644 --- a/solution/0400-0499/0476.Number Complement/Solution.go +++ b/solution/0400-0499/0476.Number Complement/Solution.go @@ -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) } \ No newline at end of file diff --git a/solution/0400-0499/0476.Number Complement/Solution.java b/solution/0400-0499/0476.Number Complement/Solution.java index f01ea4f285a59..f4f21ed6e7304 100644 --- a/solution/0400-0499/0476.Number Complement/Solution.java +++ b/solution/0400-0499/0476.Number Complement/Solution.java @@ -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); } } \ No newline at end of file diff --git a/solution/0400-0499/0476.Number Complement/Solution.py b/solution/0400-0499/0476.Number Complement/Solution.py index 86d2aed41aa0c..96b5e2d40c287 100644 --- a/solution/0400-0499/0476.Number Complement/Solution.py +++ b/solution/0400-0499/0476.Number Complement/Solution.py @@ -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) diff --git a/solution/0400-0499/0476.Number Complement/Solution2.ts b/solution/0400-0499/0476.Number Complement/Solution.ts similarity index 100% rename from solution/0400-0499/0476.Number Complement/Solution2.ts rename to solution/0400-0499/0476.Number Complement/Solution.ts diff --git a/solution/0400-0499/0476.Number Complement/Solution2.cpp b/solution/0400-0499/0476.Number Complement/Solution2.cpp deleted file mode 100644 index 6de34695822b2..0000000000000 --- a/solution/0400-0499/0476.Number Complement/Solution2.cpp +++ /dev/null @@ -1,14 +0,0 @@ -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; - } -}; \ No newline at end of file diff --git a/solution/0400-0499/0476.Number Complement/Solution2.py b/solution/0400-0499/0476.Number Complement/Solution2.py deleted file mode 100644 index 7156b7276f47c..0000000000000 --- a/solution/0400-0499/0476.Number Complement/Solution2.py +++ /dev/null @@ -1,3 +0,0 @@ -class Solution: - def findComplement(self, num: int) -> int: - return num ^ (2 ** (len(bin(num)[2:])) - 1)