Skip to content

feat: add solutions to lcci problems: No.05.03,05.07 #2671

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

Merged
merged 1 commit into from
Apr 26, 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
15 changes: 15 additions & 0 deletions lcci/05.03.Reverse Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions lcci/05.03.Reverse Bits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions lcci/05.03.Reverse Bits/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion lcci/05.06.Convert Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

### 方法一:位运算

我们将 A 和 B 进行异或运算,得到的结果中 $1$ 的个数即为需要改变的位数。
我们将 A 和 B 进行异或运算,得到的结果的二进制表示中 $1$ 的个数即为需要改变的位数。

时间复杂度 $O(\log n)$,其中 $n$ 为 A 和 B 的最大值。空间复杂度 $O(1)$。

Expand Down
28 changes: 14 additions & 14 deletions lcci/05.07.Exchange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

## 解法

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

我们可以将 $\text{num}$ 与 $\text{0x55555555}$ 进行与运算,得到的结果是 $\text{num}$ 的偶数位,然后将其左移一位。再将 $\text{num}$ 与 $\text{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\text{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -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
}
}
```
Expand Down
28 changes: 14 additions & 14 deletions lcci/05.07.Exchange/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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
}
}
```
Expand Down
16 changes: 3 additions & 13 deletions lcci/05.07.Exchange/Solution.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions lcci/05.07.Exchange/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function exchangeBits(num: number): number {
return ((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >>> 1);
}
46 changes: 20 additions & 26 deletions lcci/08.01.Three Steps Problem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -388,30 +408,4 @@ function pow(a, n) {

<!-- tabs:end -->

### 方法三

<!-- tabs:start -->

```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
```

<!-- tabs:end -->

<!-- end -->
46 changes: 20 additions & 26 deletions lcci/08.01.Three Steps Problem/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -390,30 +410,4 @@ function pow(a, n) {

<!-- tabs:end -->

### Solution 3

<!-- tabs:start -->

```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
```

<!-- tabs:end -->

<!-- end -->