Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f3f4248

Browse files
authored
feat: add rust solution to lc problem: No.0137 (doocs#1807)
No.0137.Single Number II
1 parent 6d6efeb commit f3f4248

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

solution/0100-0199/0137.Single Number II/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,24 @@ impl Solution {
259259
}
260260
```
261261

262+
```rust
263+
impl Solution {
264+
pub fn single_number(nums: Vec<i32>) -> i32 {
265+
let mut a = 0;
266+
let mut b = 0;
267+
268+
for c in nums {
269+
let aa = (!a & b & c) | (a & !b & !c);
270+
let bb = !a & (b ^ c);
271+
a = aa;
272+
b = bb;
273+
}
274+
275+
return b;
276+
}
277+
}
278+
```
279+
262280
### **C**
263281

264282
```c

solution/0100-0199/0137.Single Number II/README_EN.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,49 @@
2727

2828
## Solutions
2929

30+
**Solution 1: Bitwise Operation**
31+
32+
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.
33+
34+
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)$.
35+
36+
**Solution 2: Digital Circuit**
37+
38+
We can use a more efficient method that uses digital circuits to simulate the above bitwise operation.
39+
40+
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:
41+
42+
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;
43+
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;
44+
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.
45+
46+
We use integer $c$ to represent the number to be read in, and the truth table is as follows:
47+
48+
| $a_i$ | $b_i$ | $c_i$ | New $a_i$ | New $b_i$ |
49+
| ----- | ----- | ----- | --------- | --------- |
50+
| 0 | 0 | 0 | 0 | 0 |
51+
| 0 | 0 | 1 | 0 | 1 |
52+
| 0 | 1 | 0 | 0 | 1 |
53+
| 0 | 1 | 1 | 1 | 0 |
54+
| 1 | 0 | 0 | 1 | 0 |
55+
| 1 | 0 | 1 | 0 | 0 |
56+
57+
Based on the truth table, we can write the logical expression:
58+
59+
$$
60+
a_i = a_i' b_i c_i + a_i b_i' c_i'
61+
$$
62+
63+
and:
64+
65+
$$
66+
b_i = a_i' b_i' c_i + a_i' b_i c_i' = a_i' (b_i \oplus c_i)
67+
$$
68+
69+
The final result is $b$, because when the binary bit of $b$ is 1, it means that the number appears only once.
70+
71+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
72+
3073
<!-- tabs:start -->
3174

3275
### **Python3**
@@ -197,6 +240,24 @@ impl Solution {
197240
}
198241
```
199242

243+
```rust
244+
impl Solution {
245+
pub fn single_number(nums: Vec<i32>) -> i32 {
246+
let mut a = 0;
247+
let mut b = 0;
248+
249+
for c in nums {
250+
let aa = (!a & b & c) | (a & !b & !c);
251+
let bb = !a & (b ^ c);
252+
a = aa;
253+
b = bb;
254+
}
255+
256+
return b;
257+
}
258+
}
259+
```
260+
200261
### **C**
201262

202263
```c

0 commit comments

Comments
 (0)