diff --git a/lcci/05.03.Reverse Bits/README.md b/lcci/05.03.Reverse Bits/README.md index e75703b9004ba..e1ff0ad5ad73e 100644 --- a/lcci/05.03.Reverse Bits/README.md +++ b/lcci/05.03.Reverse Bits/README.md @@ -91,6 +91,21 @@ func reverseBits(num int) (ans int) { } ``` +```ts +function reverseBits(num: number): number { + let ans = 0; + let cnt = 0; + for (let i = 0, j = 0; i < 32; ++i) { + cnt += ((num >> i) & 1) ^ 1; + for (; cnt > 1; ++j) { + cnt -= ((num >> j) & 1) ^ 1; + } + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + ```swift class Solution { func reverseBits(_ num: Int) -> Int { diff --git a/lcci/05.03.Reverse Bits/README_EN.md b/lcci/05.03.Reverse Bits/README_EN.md index 1740cff1cf66c..e90b3b67db879 100644 --- a/lcci/05.03.Reverse Bits/README_EN.md +++ b/lcci/05.03.Reverse Bits/README_EN.md @@ -97,6 +97,21 @@ func reverseBits(num int) (ans int) { } ``` +```ts +function reverseBits(num: number): number { + let ans = 0; + let cnt = 0; + for (let i = 0, j = 0; i < 32; ++i) { + cnt += ((num >> i) & 1) ^ 1; + for (; cnt > 1; ++j) { + cnt -= ((num >> j) & 1) ^ 1; + } + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + ```swift class Solution { func reverseBits(_ num: Int) -> Int { diff --git a/lcci/05.03.Reverse Bits/Solution.ts b/lcci/05.03.Reverse Bits/Solution.ts new file mode 100644 index 0000000000000..cc8f77f1a1635 --- /dev/null +++ b/lcci/05.03.Reverse Bits/Solution.ts @@ -0,0 +1,12 @@ +function reverseBits(num: number): number { + let ans = 0; + let cnt = 0; + for (let i = 0, j = 0; i < 32; ++i) { + cnt += ((num >> i) & 1) ^ 1; + for (; cnt > 1; ++j) { + cnt -= ((num >> j) & 1) ^ 1; + } + ans = Math.max(ans, i - j + 1); + } + return ans; +} diff --git a/lcci/05.06.Convert Integer/README.md b/lcci/05.06.Convert Integer/README.md index 9c14eb779b788..df5cff8c5b78f 100644 --- a/lcci/05.06.Convert Integer/README.md +++ b/lcci/05.06.Convert Integer/README.md @@ -31,7 +31,7 @@ ### 方法一:位运算 -我们将 A 和 B 进行异或运算,得到的结果中 $1$ 的个数即为需要改变的位数。 +我们将 A 和 B 进行异或运算,得到的结果的二进制表示中 $1$ 的个数即为需要改变的位数。 时间复杂度 $O(\log n)$,其中 $n$ 为 A 和 B 的最大值。空间复杂度 $O(1)$。 diff --git a/lcci/05.07.Exchange/README.md b/lcci/05.07.Exchange/README.md index 4610acec9a67b..caf52fd4e1292 100644 --- a/lcci/05.07.Exchange/README.md +++ b/lcci/05.07.Exchange/README.md @@ -29,7 +29,11 @@ ## 解法 -### 方法一 +### 方法一:位运算 + +我们可以将 $\text{num}$ 与 $\text{0x55555555}$ 进行与运算,得到的结果是 $\text{num}$ 的偶数位,然后将其左移一位。再将 $\text{num}$ 与 $\text{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\text{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -62,21 +66,17 @@ func exchangeBits(num int) int { } ``` +```ts +function exchangeBits(num: number): number { + return ((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >>> 1); +} +``` + ```rust impl Solution { - pub fn exchange_bits(mut num: i32) -> i32 { - let mut res = 0; - let mut i = 0; - while num != 0 { - let a = num & 1; - num >>= 1; - let b = num & 1; - num >>= 1; - res |= a << (i + 1); - res |= b << i; - i += 2; - } - res + pub fn exchange_bits(num: i32) -> i32 { + let num = num as u32; + (((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >> 1)) as i32 } } ``` diff --git a/lcci/05.07.Exchange/README_EN.md b/lcci/05.07.Exchange/README_EN.md index fed05c4ebdbfe..ff390b69c7e26 100644 --- a/lcci/05.07.Exchange/README_EN.md +++ b/lcci/05.07.Exchange/README_EN.md @@ -35,7 +35,11 @@ ## Solutions -### Solution 1 +### Solution 1: Bit Manipulation + +We can perform a bitwise AND operation between `num` and `0x55555555` to get the even bits of `num`, and then shift them one bit to the left. Then, we perform a bitwise AND operation between `num` and `0xaaaaaaaa` to get the odd bits of `num`, and then shift them one bit to the right. Finally, we perform a bitwise OR operation on the two results to get the answer. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -68,21 +72,17 @@ func exchangeBits(num int) int { } ``` +```ts +function exchangeBits(num: number): number { + return ((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >>> 1); +} +``` + ```rust impl Solution { - pub fn exchange_bits(mut num: i32) -> i32 { - let mut res = 0; - let mut i = 0; - while num != 0 { - let a = num & 1; - num >>= 1; - let b = num & 1; - num >>= 1; - res |= a << (i + 1); - res |= b << i; - i += 2; - } - res + pub fn exchange_bits(num: i32) -> i32 { + let num = num as u32; + (((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >> 1)) as i32 } } ``` diff --git a/lcci/05.07.Exchange/Solution.rs b/lcci/05.07.Exchange/Solution.rs index 88859be3457b5..94ed4fe8956ea 100644 --- a/lcci/05.07.Exchange/Solution.rs +++ b/lcci/05.07.Exchange/Solution.rs @@ -1,16 +1,6 @@ impl Solution { - pub fn exchange_bits(mut num: i32) -> i32 { - let mut res = 0; - let mut i = 0; - while num != 0 { - let a = num & 1; - num >>= 1; - let b = num & 1; - num >>= 1; - res |= a << (i + 1); - res |= b << i; - i += 2; - } - res + pub fn exchange_bits(num: i32) -> i32 { + let num = num as u32; + (((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >> 1)) as i32 } } diff --git a/lcci/05.07.Exchange/Solution.ts b/lcci/05.07.Exchange/Solution.ts new file mode 100644 index 0000000000000..c55be05196719 --- /dev/null +++ b/lcci/05.07.Exchange/Solution.ts @@ -0,0 +1,3 @@ +function exchangeBits(num: number): number { + return ((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >>> 1); +} diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md index ef79528f40b37..7de99767cbcf1 100644 --- a/lcci/08.01.Three Steps Problem/README.md +++ b/lcci/08.01.Three Steps Problem/README.md @@ -200,6 +200,26 @@ class Solution: return sum(pow(a, n - 4)[0]) % mod ``` +```python +import numpy as np + + +class Solution: + def waysToStep(self, n: int) -> int: + if n < 4: + return 2 ** (n - 1) + mod = 10**9 + 7 + factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O")) + res = np.mat([(4, 2, 1)], np.dtype("O")) + n -= 4 + while n: + if n & 1: + res = res * factor % mod + factor = factor * factor % mod + n >>= 1 + return res.sum() % mod +``` + ```java class Solution { private final int mod = (int) 1e9 + 7; @@ -388,30 +408,4 @@ function pow(a, n) { -### 方法三 - - - -```python -import numpy as np - - -class Solution: - def waysToStep(self, n: int) -> int: - if n < 4: - return 2 ** (n - 1) - mod = 10**9 + 7 - factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O")) - res = np.mat([(4, 2, 1)], np.dtype("O")) - n -= 4 - while n: - if n & 1: - res = res * factor % mod - factor = factor * factor % mod - n >>= 1 - return res.sum() % mod -``` - - - diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md index fbd07e6ed265c..8cdbcfb8cb862 100644 --- a/lcci/08.01.Three Steps Problem/README_EN.md +++ b/lcci/08.01.Three Steps Problem/README_EN.md @@ -202,6 +202,26 @@ class Solution: return sum(pow(a, n - 4)[0]) % mod ``` +```python +import numpy as np + + +class Solution: + def waysToStep(self, n: int) -> int: + if n < 4: + return 2 ** (n - 1) + mod = 10**9 + 7 + factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O")) + res = np.mat([(4, 2, 1)], np.dtype("O")) + n -= 4 + while n: + if n & 1: + res = res * factor % mod + factor = factor * factor % mod + n >>= 1 + return res.sum() % mod +``` + ```java class Solution { private final int mod = (int) 1e9 + 7; @@ -390,30 +410,4 @@ function pow(a, n) { -### Solution 3 - - - -```python -import numpy as np - - -class Solution: - def waysToStep(self, n: int) -> int: - if n < 4: - return 2 ** (n - 1) - mod = 10**9 + 7 - factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O")) - res = np.mat([(4, 2, 1)], np.dtype("O")) - n -= 4 - while n: - if n & 1: - res = res * factor % mod - factor = factor * factor % mod - n >>= 1 - return res.sum() % mod -``` - - -