Skip to content

Commit 5b5bbb2

Browse files
committed
Add solution 1009
1 parent 32a5c60 commit 5b5bbb2

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package leetcode
2+
3+
func bitwiseComplement(n int) int {
4+
mask := 1
5+
for mask < n {
6+
mask = (mask << 1) + 1
7+
}
8+
return mask ^ n
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1009 struct {
9+
para1009
10+
ans1009
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1009 struct {
16+
n int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1009 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1009(t *testing.T) {
26+
27+
qs := []question1009{
28+
29+
{
30+
para1009{5},
31+
ans1009{2},
32+
},
33+
34+
{
35+
para1009{7},
36+
ans1009{0},
37+
},
38+
39+
{
40+
para1009{10},
41+
ans1009{5},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1009------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1009, q.para1009
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, bitwiseComplement(p.n))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [1009. Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)
2+
3+
4+
## 题目
5+
6+
The **complement** of an integer is the integer you get when you flip all the `0`'s to `1`'s and all the `1`'s to `0`'s in its binary representation.
7+
8+
- For example, The integer `5` is `"101"` in binary and its **complement** is `"010"` which is the integer `2`.
9+
10+
Given an integer `n`, return *its complement*.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: n = 5
16+
Output: 2
17+
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: n = 7
25+
Output: 0
26+
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
27+
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: n = 10
34+
Output: 5
35+
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
36+
37+
```
38+
39+
**Constraints:**
40+
41+
- `0 <= n < 109`
42+
43+
## 题目大意
44+
45+
每个非负整数 N 都有其二进制表示。例如, 5 可以被表示为二进制 "101",11 可以用二进制 "1011" 表示,依此类推。注意,除 N = 0 外,任何二进制表示中都不含前导零。
46+
47+
二进制的反码表示是将每个 1 改为 0 且每个 0 变为 1。例如,二进制数 "101" 的二进制反码为 "010"。
48+
49+
给你一个十进制数 N,请你返回其二进制表示的反码所对应的十进制整数。
50+
51+
## 解题思路
52+
53+
- 简单题。求一个十进制数的反码,只需要让该数和全 1 的数进行异或计算即可。所以本题重点在如何构造 mask 上。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func bitwiseComplement(n int) int {
61+
mask := 1
62+
for mask < n {
63+
mask = (mask << 1) + 1
64+
}
65+
return mask ^ n
66+
}
67+
```

0 commit comments

Comments
 (0)