Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b429b0

Browse files
committed
feat: add solutions to lc problems: No.0476,1009
No.0476.Number complement No.1009.Complement of Base 10 Integer
1 parent f56cac4 commit 7b429b0

File tree

11 files changed

+415
-22
lines changed

11 files changed

+415
-22
lines changed

‎solution/0400-0499/0476.Number Complement/README.md‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li>本题与 1009 <a href="https://leetcode-cn.com/problems/complement-of-base-10-integer/">https://leetcode-cn.com/problems/complement-of-base-10-integer/</a> 相同</li>
4141
</ul>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
@@ -52,15 +51,92 @@
5251
<!-- 这里可写当前语言的特殊实现逻辑 -->
5352

5453
```python
55-
54+
class Solution:
55+
def findComplement(self, num: int) -> int:
56+
ans = 0
57+
find = False
58+
for i in range(30, -1, -1):
59+
b = num & (1 << i)
60+
if not find and b == 0:
61+
continue
62+
find = True
63+
if b == 0:
64+
ans |= (1 << i)
65+
return ans
5666
```
5767

5868
### **Java**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```java
73+
class Solution {
74+
public int findComplement(int num) {
75+
int ans = 0;
76+
boolean find = false;
77+
for (int i = 30; i >= 0; --i) {
78+
int b = num & (1 << i);
79+
if (!find && b == 0) {
80+
continue;
81+
}
82+
find = true;
83+
if (b == 0) {
84+
ans |= (1 << i);
85+
}
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int findComplement(int num) {
98+
int full = pow(2, int(log2(num)) + 1) - 1;
99+
return full ^ num;
100+
}
101+
};
102+
```
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int findComplement(int num) {
108+
int ans = 0;
109+
bool find = false;
110+
for (int i = 30; i >= 0; --i)
111+
{
112+
int b = num & (1 << i);
113+
if (!find && b == 0) continue;
114+
find = true;
115+
if (b == 0) ans |= (1 << i);
116+
}
117+
return ans;
118+
}
119+
};
120+
```
63121

122+
### **Go**
123+
124+
```go
125+
func findComplement(num int) int {
126+
ans := 0
127+
find := false
128+
for i := 30; i >= 0; i-- {
129+
b := num & (1 << i)
130+
if !find && b == 0 {
131+
continue
132+
}
133+
find = true
134+
if b == 0 {
135+
ans |= (1 << i)
136+
}
137+
}
138+
return ans
139+
}
64140
```
65141

66142
### **...**

‎solution/0400-0499/0476.Number Complement/README_EN.md‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,97 @@
3333
<li>This question is the same as 1009:&nbsp;<a href="https://leetcode.com/problems/complement-of-base-10-integer/">https://leetcode.com/problems/complement-of-base-10-integer/</a></li>
3434
</ul>
3535

36-
3736
## Solutions
3837

3938
<!-- tabs:start -->
4039

4140
### **Python3**
4241

4342
```python
44-
43+
class Solution:
44+
def findComplement(self, num: int) -> int:
45+
ans = 0
46+
find = False
47+
for i in range(30, -1, -1):
48+
b = num & (1 << i)
49+
if not find and b == 0:
50+
continue
51+
find = True
52+
if b == 0:
53+
ans |= (1 << i)
54+
return ans
4555
```
4656

4757
### **Java**
4858

4959
```java
60+
class Solution {
61+
public int findComplement(int num) {
62+
int ans = 0;
63+
boolean find = false;
64+
for (int i = 30; i >= 0; --i) {
65+
int b = num & (1 << i);
66+
if (!find && b == 0) {
67+
continue;
68+
}
69+
find = true;
70+
if (b == 0) {
71+
ans |= (1 << i);
72+
}
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int findComplement(int num) {
85+
int full = pow(2, int(log2(num)) + 1) - 1;
86+
return full ^ num;
87+
}
88+
};
89+
```
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int findComplement(int num) {
95+
int ans = 0;
96+
bool find = false;
97+
for (int i = 30; i >= 0; --i)
98+
{
99+
int b = num & (1 << i);
100+
if (!find && b == 0) continue;
101+
find = true;
102+
if (b == 0) ans |= (1 << i);
103+
}
104+
return ans;
105+
}
106+
};
107+
```
50108

109+
### **Go**
110+
111+
```go
112+
func findComplement(num int) int {
113+
ans := 0
114+
find := false
115+
for i := 30; i >= 0; i-- {
116+
b := num & (1 << i)
117+
if !find && b == 0 {
118+
continue
119+
}
120+
find = true
121+
if b == 0 {
122+
ans |= (1 << i)
123+
}
124+
}
125+
return ans
126+
}
51127
```
52128

53129
### **...**
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
class Solution {
2-
public:
2+
public:
33
int findComplement(int num) {
4-
int full = pow(2, int(log2(num)) + 1) - 1;
5-
return full ^ num;
4+
int ans = 0;
5+
bool find = false;
6+
for (int i = 30; i >= 0; --i)
7+
{
8+
int b = num & (1 << i);
9+
if (!find && b == 0) continue;
10+
find = true;
11+
if (b == 0) ans |= (1 << i);
12+
}
13+
return ans;
614
}
715
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func findComplement(num int) int {
2+
ans := 0
3+
find := false
4+
for i := 30; i >= 0; i-- {
5+
b := num & (1 << i)
6+
if !find && b == 0 {
7+
continue
8+
}
9+
find = true
10+
if b == 0 {
11+
ans |= (1 << i)
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findComplement(int num) {
3+
int ans = 0;
4+
boolean find = false;
5+
for (int i = 30; i >= 0; --i) {
6+
int b = num & (1 << i);
7+
if (!find && b == 0) {
8+
continue;
9+
}
10+
find = true;
11+
if (b == 0) {
12+
ans |= (1 << i);
13+
}
14+
}
15+
return ans;
16+
}
17+
}

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
<li>本题与 476:<a href="https://leetcode-cn.com/problems/number-complement/">https://leetcode-cn.com/problems/number-complement/</a> 相同</li>
4848
</ol>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
@@ -59,15 +58,91 @@
5958
<!-- 这里可写当前语言的特殊实现逻辑 -->
6059

6160
```python
62-
61+
class Solution:
62+
def bitwiseComplement(self, n: int) -> int:
63+
if n == 0:
64+
return 1
65+
ans = 0
66+
find = False
67+
for i in range(30, -1, -1):
68+
b = n & (1 << i)
69+
if not find and b == 0:
70+
continue
71+
find = True
72+
if b == 0:
73+
ans |= (1 << i)
74+
return ans
6375
```
6476

6577
### **Java**
6678

6779
<!-- 这里可写当前语言的特殊实现逻辑 -->
6880

6981
```java
82+
class Solution {
83+
public int bitwiseComplement(int n) {
84+
if (n == 0) {
85+
return 1;
86+
}
87+
int ans = 0;
88+
boolean find = false;
89+
for (int i = 30; i >= 0; --i) {
90+
int b = n & (1 << i);
91+
if (!find && b == 0) {
92+
continue;
93+
}
94+
find = true;
95+
if (b == 0) {
96+
ans |= (1 << i);
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int bitwiseComplement(int n) {
110+
if (n == 0) return 1;
111+
int ans = 0;
112+
bool find = false;
113+
for (int i = 30; i >= 0; --i)
114+
{
115+
int b = n & (1 << i);
116+
if (!find && b == 0) continue;
117+
find = true;
118+
if (b == 0) ans |= (1 << i);
119+
}
120+
return ans;
121+
}
122+
};
123+
```
70124
125+
### **Go**
126+
127+
```go
128+
func bitwiseComplement(n int) int {
129+
if n == 0 {
130+
return 1
131+
}
132+
ans := 0
133+
find := false
134+
for i := 30; i >= 0; i-- {
135+
b := n & (1 << i)
136+
if !find && b == 0 {
137+
continue
138+
}
139+
find = true
140+
if b == 0 {
141+
ans |= (1 << i)
142+
}
143+
}
144+
return ans
145+
}
71146
```
72147

73148
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /