Skip to content

Commit 1d3a4dc

Browse files
authored
feat: add solutions to lc problem: No.1009 (#3843)
No.1009.Complement of Base 10 Integer
1 parent 6106d34 commit 1d3a4dc

File tree

8 files changed

+179
-128
lines changed

8 files changed

+179
-128
lines changed

solution/1000-1099/1009.Complement of Base 10 Integer/README.md

+65-42
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ tags:
6565

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

68-
### 方法一
68+
### 方法一:位运算
69+
70+
我们首先判断 $n$ 是否为 $0$,如果是,则返回 $1$。
71+
72+
接着我们定义两个变量 $\textit{ans}$ 和 $i$,初始化为 $0$。然后我们遍历 $n$,在每次遍历中,我们将 $\textit{ans}$ 的第 $i$ 位设置为 $n$ 的第 $i$ 位取反,然后将 $i$ 加 $1$,并且$n$ 右移 $1$ 位。
73+
74+
最后返回 $\textit{ans}$ 即可。
75+
76+
时间复杂度 $O(\log n)$,其中 $n$ 为给定的十进制数。空间复杂度 $O(1)$。
6977

7078
<!-- tabs:start -->
7179

@@ -76,15 +84,11 @@ class Solution:
7684
def bitwiseComplement(self, n: int) -> int:
7785
if n == 0:
7886
return 1
79-
ans = 0
80-
find = False
81-
for i in range(30, -1, -1):
82-
b = n & (1 << i)
83-
if not find and b == 0:
84-
continue
85-
find = True
86-
if b == 0:
87-
ans |= 1 << i
87+
ans = i = 0
88+
while n:
89+
ans |= ((n & 1 ^ 1) << i)
90+
i += 1
91+
n >>= 1
8892
return ans
8993
```
9094

@@ -96,17 +100,10 @@ class Solution {
96100
if (n == 0) {
97101
return 1;
98102
}
99-
int ans = 0;
100-
boolean find = false;
101-
for (int i = 30; i >= 0; --i) {
102-
int b = n & (1 << i);
103-
if (!find && b == 0) {
104-
continue;
105-
}
106-
find = true;
107-
if (b == 0) {
108-
ans |= (1 << i);
109-
}
103+
int ans = 0, i = 0;
104+
while (n != 0) {
105+
ans |= (n & 1 ^ 1) << (i++);
106+
n >>= 1;
110107
}
111108
return ans;
112109
}
@@ -119,14 +116,13 @@ class Solution {
119116
class Solution {
120117
public:
121118
int bitwiseComplement(int n) {
122-
if (n == 0) return 1;
123-
int ans = 0;
124-
bool find = false;
125-
for (int i = 30; i >= 0; --i) {
126-
int b = n & (1 << i);
127-
if (!find && b == 0) continue;
128-
find = true;
129-
if (b == 0) ans |= (1 << i);
119+
if (n == 0) {
120+
return 1;
121+
}
122+
int ans = 0, i = 0;
123+
while (n != 0) {
124+
ans |= (n & 1 ^ 1) << (i++);
125+
n >>= 1;
130126
}
131127
return ans;
132128
}
@@ -136,23 +132,50 @@ public:
136132
#### Go
137133
138134
```go
139-
func bitwiseComplement(n int) int {
135+
func bitwiseComplement(n int) (ans int) {
140136
if n == 0 {
141137
return 1
142138
}
143-
ans := 0
144-
find := false
145-
for i := 30; i >= 0; i-- {
146-
b := n & (1 << i)
147-
if !find && b == 0 {
148-
continue
149-
}
150-
find = true
151-
if b == 0 {
152-
ans |= (1 << i)
153-
}
139+
for i := 0; n != 0; n >>= 1 {
140+
ans |= (n&1 ^ 1) << i
141+
i++
154142
}
155-
return ans
143+
return
144+
}
145+
```
146+
147+
#### TypeScript
148+
149+
```ts
150+
function bitwiseComplement(n: number): number {
151+
if (n === 0) {
152+
return 1;
153+
}
154+
let ans = 0;
155+
for (let i = 0; n; n >>= 1) {
156+
ans |= ((n & 1) ^ 1) << i++;
157+
}
158+
return ans;
159+
}
160+
```
161+
162+
#### Rust
163+
164+
```rust
165+
impl Solution {
166+
pub fn bitwise_complement(mut n: i32) -> i32 {
167+
if n == 0 {
168+
return 1;
169+
}
170+
let mut ans = 0;
171+
let mut i = 0;
172+
while n != 0 {
173+
ans |= ((n & 1) ^ 1) << i;
174+
n >>= 1;
175+
i += 1;
176+
}
177+
ans
178+
}
156179
}
157180
```
158181

solution/1000-1099/1009.Complement of Base 10 Integer/README_EN.md

+65-42
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Bit Manipulation
71+
72+
First, we check if $n$ is $0$. If it is, we return $1$.
73+
74+
Next, we define two variables $\textit{ans}$ and $i$, both initialized to $0$. Then we iterate through $n$. In each iteration, we set the $i$-th bit of $\textit{ans}$ to the inverse of the $i$-th bit of $n$, increment $i$ by $1$, and right shift $n$ by $1$.
75+
76+
Finally, we return $\textit{ans}$.
77+
78+
The time complexity is $O(\log n)$, where $n$ is the given decimal number. The space complexity is $O(1)$.
7179

7280
<!-- tabs:start -->
7381

@@ -78,15 +86,11 @@ class Solution:
7886
def bitwiseComplement(self, n: int) -> int:
7987
if n == 0:
8088
return 1
81-
ans = 0
82-
find = False
83-
for i in range(30, -1, -1):
84-
b = n & (1 << i)
85-
if not find and b == 0:
86-
continue
87-
find = True
88-
if b == 0:
89-
ans |= 1 << i
89+
ans = i = 0
90+
while n:
91+
ans |= ((n & 1 ^ 1) << i)
92+
i += 1
93+
n >>= 1
9094
return ans
9195
```
9296

@@ -98,17 +102,10 @@ class Solution {
98102
if (n == 0) {
99103
return 1;
100104
}
101-
int ans = 0;
102-
boolean find = false;
103-
for (int i = 30; i >= 0; --i) {
104-
int b = n & (1 << i);
105-
if (!find && b == 0) {
106-
continue;
107-
}
108-
find = true;
109-
if (b == 0) {
110-
ans |= (1 << i);
111-
}
105+
int ans = 0, i = 0;
106+
while (n != 0) {
107+
ans |= (n & 1 ^ 1) << (i++);
108+
n >>= 1;
112109
}
113110
return ans;
114111
}
@@ -121,14 +118,13 @@ class Solution {
121118
class Solution {
122119
public:
123120
int bitwiseComplement(int n) {
124-
if (n == 0) return 1;
125-
int ans = 0;
126-
bool find = false;
127-
for (int i = 30; i >= 0; --i) {
128-
int b = n & (1 << i);
129-
if (!find && b == 0) continue;
130-
find = true;
131-
if (b == 0) ans |= (1 << i);
121+
if (n == 0) {
122+
return 1;
123+
}
124+
int ans = 0, i = 0;
125+
while (n != 0) {
126+
ans |= (n & 1 ^ 1) << (i++);
127+
n >>= 1;
132128
}
133129
return ans;
134130
}
@@ -138,23 +134,50 @@ public:
138134
#### Go
139135
140136
```go
141-
func bitwiseComplement(n int) int {
137+
func bitwiseComplement(n int) (ans int) {
142138
if n == 0 {
143139
return 1
144140
}
145-
ans := 0
146-
find := false
147-
for i := 30; i >= 0; i-- {
148-
b := n & (1 << i)
149-
if !find && b == 0 {
150-
continue
151-
}
152-
find = true
153-
if b == 0 {
154-
ans |= (1 << i)
155-
}
141+
for i := 0; n != 0; n >>= 1 {
142+
ans |= (n&1 ^ 1) << i
143+
i++
156144
}
157-
return ans
145+
return
146+
}
147+
```
148+
149+
#### TypeScript
150+
151+
```ts
152+
function bitwiseComplement(n: number): number {
153+
if (n === 0) {
154+
return 1;
155+
}
156+
let ans = 0;
157+
for (let i = 0; n; n >>= 1) {
158+
ans |= ((n & 1) ^ 1) << i++;
159+
}
160+
return ans;
161+
}
162+
```
163+
164+
#### Rust
165+
166+
```rust
167+
impl Solution {
168+
pub fn bitwise_complement(mut n: i32) -> i32 {
169+
if n == 0 {
170+
return 1;
171+
}
172+
let mut ans = 0;
173+
let mut i = 0;
174+
while n != 0 {
175+
ans |= ((n & 1) ^ 1) << i;
176+
n >>= 1;
177+
i += 1;
178+
}
179+
ans
180+
}
158181
}
159182
```
160183

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution {
22
public:
33
int bitwiseComplement(int n) {
4-
if (n == 0) return 1;
5-
int ans = 0;
6-
bool find = false;
7-
for (int i = 30; i >= 0; --i) {
8-
int b = n & (1 << i);
9-
if (!find && b == 0) continue;
10-
find = true;
11-
if (b == 0) ans |= (1 << i);
4+
if (n == 0) {
5+
return 1;
6+
}
7+
int ans = 0, i = 0;
8+
while (n != 0) {
9+
ans |= (n & 1 ^ 1) << (i++);
10+
n >>= 1;
1211
}
1312
return ans;
1413
}
15-
};
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
func bitwiseComplement(n int) int {
1+
func bitwiseComplement(n int) (ans int) {
22
if n == 0 {
33
return 1
44
}
5-
ans := 0
6-
find := false
7-
for i := 30; i >= 0; i-- {
8-
b := n & (1 << i)
9-
if !find && b == 0 {
10-
continue
11-
}
12-
find = true
13-
if b == 0 {
14-
ans |= (1 << i)
15-
}
5+
for i := 0; n != 0; n >>= 1 {
6+
ans |= (n&1 ^ 1) << i
7+
i++
168
}
17-
return ans
18-
}
9+
return
10+
}

solution/1000-1099/1009.Complement of Base 10 Integer/Solution.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ public int bitwiseComplement(int n) {
33
if (n == 0) {
44
return 1;
55
}
6-
int ans = 0;
7-
boolean find = false;
8-
for (int i = 30; i >= 0; --i) {
9-
int b = n & (1 << i);
10-
if (!find && b == 0) {
11-
continue;
12-
}
13-
find = true;
14-
if (b == 0) {
15-
ans |= (1 << i);
16-
}
6+
int ans = 0, i = 0;
7+
while (n != 0) {
8+
ans |= (n & 1 ^ 1) << (i++);
9+
n >>= 1;
1710
}
1811
return ans;
1912
}
20-
}
13+
}

solution/1000-1099/1009.Complement of Base 10 Integer/Solution.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ class Solution:
22
def bitwiseComplement(self, n: int) -> int:
33
if n == 0:
44
return 1
5-
ans = 0
6-
find = False
7-
for i in range(30, -1, -1):
8-
b = n & (1 << i)
9-
if not find and b == 0:
10-
continue
11-
find = True
12-
if b == 0:
13-
ans |= 1 << i
5+
ans = i = 0
6+
while n:
7+
ans |= (n & 1 ^ 1) << i
8+
i += 1
9+
n >>= 1
1410
return ans

0 commit comments

Comments
 (0)