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: add rust solution to lc problem: No.0137 #1807

Merged
merged 1 commit into from
Oct 15, 2023
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
18 changes: 18 additions & 0 deletions solution/0100-0199/0137.Single Number II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ impl Solution {
}
```

```rust
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let mut a = 0;
let mut b = 0;

for c in nums {
let aa = (!a & b & c) | (a & !b & !c);
let bb = !a & (b ^ c);
a = aa;
b = bb;
}

return b;
}
}
```

### **C**

```c
Expand Down
61 changes: 61 additions & 0 deletions solution/0100-0199/0137.Single Number II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@

## Solutions

**Solution 1: Bitwise Operation**

We can enumerate each binary bit $i$, and for each binary bit, we calculate the sum of all numbers on that bit. If the sum of the numbers on that bit can be divided by 3, then the number that only appears once on that bit is 0, otherwise it is 1.

The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array and the range of elements in the array, respectively. The space complexity is $O(1)$.

**Solution 2: Digital Circuit**

We can use a more efficient method that uses digital circuits to simulate the above bitwise operation.

Each binary bit of an integer can only represent 2 states, 0 or 1. However, we need to represent the sum of the $i$-th bit of all integers traversed so far modulo 3. Therefore, we can use two integers $a$ and $b$ to represent it. There are three possible cases:

1. The $i$-th bit of integer $a$ is 0 and the $i$-th bit of integer $b$ is 0, which means the modulo 3 result is 0;
2. The $i$-th bit of integer $a$ is 0 and the $i$-th bit of integer $b$ is 1, which means the modulo 3 result is 1;
3. The $i$-th bit of integer $a$ is 1 and the $i$-th bit of integer $b$ is 0, which means the modulo 3 result is 2.

We use integer $c$ to represent the number to be read in, and the truth table is as follows:

| $a_i$ | $b_i$ | $c_i$ | New $a_i$ | New $b_i$ |
| ----- | ----- | ----- | --------- | --------- |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |

Based on the truth table, we can write the logical expression:

$$
a_i = a_i' b_i c_i + a_i b_i' c_i'
$$

and:

$$
b_i = a_i' b_i' c_i + a_i' b_i c_i' = a_i' (b_i \oplus c_i)
$$

The final result is $b$, because when the binary bit of $b$ is 1, it means that the number appears only once.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -197,6 +240,24 @@ impl Solution {
}
```

```rust
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let mut a = 0;
let mut b = 0;

for c in nums {
let aa = (!a & b & c) | (a & !b & !c);
let bb = !a & (b ^ c);
a = aa;
b = bb;
}

return b;
}
}
```

### **C**

```c
Expand Down