File tree Expand file tree Collapse file tree 11 files changed +415
-22
lines changed
0400-0499/0476.Number Complement
1000-1099/1009.Complement of Base 10 Integer Expand file tree Collapse file tree 11 files changed +415
-22
lines changed Original file line number Diff line number Diff line change 40
40
<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>
41
41
</ul >
42
42
43
-
44
43
## 解法
45
44
46
45
<!-- 这里可写通用的实现逻辑 -->
52
51
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
52
54
53
``` 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
56
66
```
57
67
58
68
### ** Java**
59
69
60
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
71
62
72
``` 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
+ ```
63
121
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
+ }
64
140
```
65
141
66
142
### ** ...**
Original file line number Diff line number Diff line change 33
33
<li>This question is the same as 1009: <a href="https://leetcode.com/problems/complement-of-base-10-integer/">https://leetcode.com/problems/complement-of-base-10-integer/</a></li>
34
34
</ul >
35
35
36
-
37
36
## Solutions
38
37
39
38
<!-- tabs:start -->
40
39
41
40
### ** Python3**
42
41
43
42
``` 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
45
55
```
46
56
47
57
### ** Java**
48
58
49
59
``` 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
+ ```
50
108
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
+ }
51
127
```
52
128
53
129
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public:
2
+ public:
3
3
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;
6
14
}
7
15
};
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 47
47
<li>本题与 476:<a href="https://leetcode-cn.com/problems/number-complement/">https://leetcode-cn.com/problems/number-complement/</a> 相同</li>
48
48
</ol >
49
49
50
-
51
50
## 解法
52
51
53
52
<!-- 这里可写通用的实现逻辑 -->
59
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
59
61
60
``` 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
63
75
```
64
76
65
77
### ** Java**
66
78
67
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
80
69
81
``` 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
+ ```
70
124
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
+ }
71
146
```
72
147
73
148
### ** ...**
You can’t perform that action at this time.
0 commit comments