Skip to content

Commit 5fc5191

Browse files
committed
feat: add rust solution to lc problem: No.1611
1 parent 8b8c0d0 commit 5fc5191

File tree

8 files changed

+51
-161
lines changed

8 files changed

+51
-161
lines changed

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README.md

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -157,72 +157,18 @@ function minimumOneBitOperations(n: number): number {
157157
}
158158
```
159159

160-
<!-- tabs:end -->
161-
162-
<!-- solution:end -->
163-
164-
<!-- solution:start -->
165-
166-
### 方法二
167-
168-
<!-- tabs:start -->
169-
170-
#### Python3
171-
172-
```python
173-
class Solution:
174-
def minimumOneBitOperations(self, n: int) -> int:
175-
if n == 0:
176-
return 0
177-
return n ^ self.minimumOneBitOperations(n >> 1)
178-
```
160+
#### Rust
179161

180-
#### Java
181-
182-
```java
183-
class Solution {
184-
public int minimumOneBitOperations(int n) {
185-
if (n == 0) {
186-
return 0;
187-
}
188-
return n ^ minimumOneBitOperations(n >> 1);
189-
}
190-
}
191-
```
192-
193-
#### C++
194-
195-
```cpp
196-
class Solution {
197-
public:
198-
int minimumOneBitOperations(int n) {
199-
if (n == 0) {
200-
return 0;
162+
```rust
163+
impl Solution {
164+
pub fn minimum_one_bit_operations(mut n: i32) -> i32 {
165+
let mut ans = 0;
166+
while n > 0 {
167+
ans ^= n;
168+
n >>= 1;
201169
}
202-
return n ^ minimumOneBitOperations(n >> 1);
203-
}
204-
};
205-
```
206-
207-
#### Go
208-
209-
```go
210-
func minimumOneBitOperations(n int) int {
211-
if n == 0 {
212-
return 0
213-
}
214-
return n ^ minimumOneBitOperations(n>>1)
215-
}
216-
```
217-
218-
#### TypeScript
219-
220-
```ts
221-
function minimumOneBitOperations(n: number): number {
222-
if (n === 0) {
223-
return 0;
170+
ans
224171
}
225-
return n ^ minimumOneBitOperations(n >> 1);
226172
}
227173
```
228174

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README_EN.md

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,29 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Gray Code Inverse Transform (Gray Code to Binary Code)
69+
70+
This problem essentially asks for the inverse transformation of Gray code at position $n$, i.e., constructing the original number from the Gray code.
71+
72+
Let's first review how to convert binary code to binary Gray code. The rule is to keep the most significant bit of the binary code as the most significant bit of the Gray code, while the second most significant bit of the Gray code is obtained by XORing the most significant bit and the second most significant bit of the binary code. The remaining bits of the Gray code are computed similarly to the second most significant bit.
73+
74+
Suppose a binary number is represented as $B_{n-1}B_{n-2}...B_2B_1B_0$, and its Gray code representation is $G_{n-1}G_{n-2}...G_2G_1G_0$. The most significant bit is kept, so $G_{n-1} = B_{n-1}$; and for other bits $G_i = B_{i+1} \oplus B_{i}$, where $i=0,1,2..,n-2$.
75+
76+
So what is the inverse transformation from Gray code to binary code?
77+
78+
We can observe that the most significant bit of the Gray code is kept, so $B_{n-1} = G_{n-1}$; and $B_{n-2} = G_{n-2} \oplus B_{n-1} = G_{n-2} \oplus G_{n-1}$; and for other bits $B_i = G_{i} \oplus G_{i+1} \cdots \oplus G_{n-1}$, where $i=0,1,2..,n-2$. Therefore, we can use the following function $rev(x)$ to obtain its binary code:
79+
80+
```java
81+
int rev(int x) {
82+
int n = 0;
83+
for (; x != 0; x >>= 1) {
84+
n ^= x;
85+
}
86+
return n;
87+
}
88+
```
89+
90+
The time complexity is $O(\log n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$.
6991

7092
<!-- tabs:start -->
7193

@@ -133,72 +155,18 @@ function minimumOneBitOperations(n: number): number {
133155
}
134156
```
135157

136-
<!-- tabs:end -->
137-
138-
<!-- solution:end -->
139-
140-
<!-- solution:start -->
141-
142-
### Solution 2
143-
144-
<!-- tabs:start -->
145-
146-
#### Python3
147-
148-
```python
149-
class Solution:
150-
def minimumOneBitOperations(self, n: int) -> int:
151-
if n == 0:
152-
return 0
153-
return n ^ self.minimumOneBitOperations(n >> 1)
154-
```
155-
156-
#### Java
157-
158-
```java
159-
class Solution {
160-
public int minimumOneBitOperations(int n) {
161-
if (n == 0) {
162-
return 0;
163-
}
164-
return n ^ minimumOneBitOperations(n >> 1);
165-
}
166-
}
167-
```
168-
169-
#### C++
158+
#### Rust
170159

171-
```cpp
172-
class Solution {
173-
public:
174-
int minimumOneBitOperations(int n) {
175-
if (n == 0) {
176-
return 0;
160+
```rust
161+
impl Solution {
162+
pub fn minimum_one_bit_operations(mut n: i32) -> i32 {
163+
let mut ans = 0;
164+
while n > 0 {
165+
ans ^= n;
166+
n >>= 1;
177167
}
178-
return n ^ minimumOneBitOperations(n >> 1);
179-
}
180-
};
181-
```
182-
183-
#### Go
184-
185-
```go
186-
func minimumOneBitOperations(n int) int {
187-
if n == 0 {
188-
return 0
189-
}
190-
return n ^ minimumOneBitOperations(n>>1)
191-
}
192-
```
193-
194-
#### TypeScript
195-
196-
```ts
197-
function minimumOneBitOperations(n: number): number {
198-
if (n === 0) {
199-
return 0;
168+
ans
200169
}
201-
return n ^ minimumOneBitOperations(n >> 1);
202170
}
203171
```
204172

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn minimum_one_bit_operations(mut n: i32) -> i32 {
3+
let mut ans = 0;
4+
while n > 0 {
5+
ans ^= n;
6+
n >>= 1;
7+
}
8+
ans
9+
}
10+
}

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.cpp

Lines changed: 0 additions & 9 deletions
This file was deleted.

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)