diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README.md b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README.md index dd756aaf150e2..8e11e91b403af 100644 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README.md +++ b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README.md @@ -157,72 +157,18 @@ function minimumOneBitOperations(n: number): number { } ``` - - - - - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def minimumOneBitOperations(self, n: int) -> int: - if n == 0: - return 0 - return n ^ self.minimumOneBitOperations(n >> 1) -``` +#### Rust -#### Java - -```java -class Solution { - public int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; - } - return n ^ minimumOneBitOperations(n >> 1); - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; +```rust +impl Solution { + pub fn minimum_one_bit_operations(mut n: i32) -> i32 { + let mut ans = 0; + while n > 0 { + ans ^= n; + n >>= 1; } - return n ^ minimumOneBitOperations(n >> 1); - } -}; -``` - -#### Go - -```go -func minimumOneBitOperations(n int) int { - if n == 0 { - return 0 - } - return n ^ minimumOneBitOperations(n>>1) -} -``` - -#### TypeScript - -```ts -function minimumOneBitOperations(n: number): number { - if (n === 0) { - return 0; + ans } - return n ^ minimumOneBitOperations(n >> 1); } ``` diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README_EN.md b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README_EN.md index 20fdd6eecedb9..e803de881e356 100644 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README_EN.md +++ b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/README_EN.md @@ -65,7 +65,29 @@ tags: -### Solution 1 +### Solution 1: Gray Code Inverse Transform (Gray Code to Binary Code) + +This problem essentially asks for the inverse transformation of Gray code at position $n$, i.e., constructing the original number from the Gray code. + +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. + +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$. + +So what is the inverse transformation from Gray code to binary code? + +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: + +```java +int rev(int x) { + int n = 0; + for (; x != 0; x >>= 1) { + n ^= x; + } + return n; +} +``` + +The time complexity is $O(\log n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$. @@ -133,72 +155,18 @@ function minimumOneBitOperations(n: number): number { } ``` - - - - - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def minimumOneBitOperations(self, n: int) -> int: - if n == 0: - return 0 - return n ^ self.minimumOneBitOperations(n >> 1) -``` - -#### Java - -```java -class Solution { - public int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; - } - return n ^ minimumOneBitOperations(n >> 1); - } -} -``` - -#### C++ +#### Rust -```cpp -class Solution { -public: - int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; +```rust +impl Solution { + pub fn minimum_one_bit_operations(mut n: i32) -> i32 { + let mut ans = 0; + while n > 0 { + ans ^= n; + n >>= 1; } - return n ^ minimumOneBitOperations(n >> 1); - } -}; -``` - -#### Go - -```go -func minimumOneBitOperations(n int) int { - if n == 0 { - return 0 - } - return n ^ minimumOneBitOperations(n>>1) -} -``` - -#### TypeScript - -```ts -function minimumOneBitOperations(n: number): number { - if (n === 0) { - return 0; + ans } - return n ^ minimumOneBitOperations(n >> 1); } ``` diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution.rs b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution.rs new file mode 100644 index 0000000000000..d5b38ff0f5439 --- /dev/null +++ b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn minimum_one_bit_operations(mut n: i32) -> i32 { + let mut ans = 0; + while n > 0 { + ans ^= n; + n >>= 1; + } + ans + } +} diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.cpp b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.cpp deleted file mode 100644 index 8d69eaa2d4a84..0000000000000 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.cpp +++ /dev/null @@ -1,9 +0,0 @@ -class Solution { -public: - int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; - } - return n ^ minimumOneBitOperations(n >> 1); - } -}; \ No newline at end of file diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.go b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.go deleted file mode 100644 index cb784a51c1f28..0000000000000 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.go +++ /dev/null @@ -1,6 +0,0 @@ -func minimumOneBitOperations(n int) int { - if n == 0 { - return 0 - } - return n ^ minimumOneBitOperations(n>>1) -} \ No newline at end of file diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.java b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.java deleted file mode 100644 index a7f663e8c30c4..0000000000000 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.java +++ /dev/null @@ -1,8 +0,0 @@ -class Solution { - public int minimumOneBitOperations(int n) { - if (n == 0) { - return 0; - } - return n ^ minimumOneBitOperations(n >> 1); - } -} \ No newline at end of file diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.py b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.py deleted file mode 100644 index 5b7412abc10a3..0000000000000 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.py +++ /dev/null @@ -1,5 +0,0 @@ -class Solution: - def minimumOneBitOperations(self, n: int) -> int: - if n == 0: - return 0 - return n ^ self.minimumOneBitOperations(n >> 1) diff --git a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.ts b/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.ts deleted file mode 100644 index bf6c4dd61d69a..0000000000000 --- a/solution/1600-1699/1611.Minimum One Bit Operations to Make Integers Zero/Solution2.ts +++ /dev/null @@ -1,6 +0,0 @@ -function minimumOneBitOperations(n: number): number { - if (n === 0) { - return 0; - } - return n ^ minimumOneBitOperations(n >> 1); -}