|
27 | 27 |
|
28 | 28 | ## Solutions
|
29 | 29 |
|
| 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 | + |
30 | 73 | <!-- tabs:start -->
|
31 | 74 |
|
32 | 75 | ### **Python3**
|
@@ -197,6 +240,24 @@ impl Solution {
|
197 | 240 | }
|
198 | 241 | ```
|
199 | 242 |
|
| 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 | + |
200 | 261 | ### **C**
|
201 | 262 |
|
202 | 263 | ```c
|
|
0 commit comments