Skip to content

Commit 19c5232

Browse files
committed
Add solution and test-cases for problem 3370
1 parent f03226b commit 19c5232

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3370.Smallest Number With All Set Bits][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a positive number `n`.
5+
6+
Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only set bits
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 5
12+
13+
Output: 7
14+
15+
Explanation:
16+
17+
The binary representation of 7 is "111".
18+
```
19+
20+
**Example 2:**
21+
1322
```
23+
Input: n = 10
24+
25+
Output: 15
1426
15-
## 题意
16-
> ...
27+
Explanation:
28+
29+
The binary representation of 15 is "1111".
30+
```
1731

18-
## 题解
32+
**Example 3:**
1933

20-
### 思路1
21-
> ...
22-
Smallest Number With All Set Bits
23-
```go
2434
```
35+
Input: n = 3
2536
37+
Output: 3
38+
39+
Explanation:
40+
41+
The binary representation of 3 is "11".
42+
```
2643

2744
## 结语
2845

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
base := 2
5+
for i := 0; i < 10; i++ {
6+
if base-1 >= n {
7+
return base - 1
8+
}
9+
base *= 2
10+
}
11+
return 0
512
}

leetcode/3301-3400/3370.Smallest-Number-With-All-Set-Bits/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 5, 7},
17+
{"TestCase2", 10, 15},
18+
{"TestCase3", 3, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)