Skip to content

Commit 8383282

Browse files
authored
Merge pull request #1258 from 0xff-dev/868
Add solution and test-cases for problem 868
2 parents 9370fa2 + 9881f25 commit 8383282

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

leetcode/801-900/0868.Binary-Gap/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [868.Binary Gap][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+
Given a positive integer `n`, find and return the **longest distance** between any two **adjacent** `1`'s in the binary representation of `n`. If there are no two adjacent `1`'s, return `0`.
5+
6+
Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `1`'s is the absolute difference between their bit positions. For example, the two `1`'s in `"1001"` have a distance of 3.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 22
12+
Output: 2
13+
Explanation: 22 in binary is "10110".
14+
The first adjacent pair of 1's is "10110" with a distance of 2.
15+
The second adjacent pair of 1's is "10110" with a distance of 1.
16+
The answer is the largest of these two distances, which is 2.
17+
Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Binary Gap
23-
```go
22+
```
23+
Input: n = 8
24+
Output: 0
25+
Explanation: 8 in binary is "1000".
26+
There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
2427
```
2528

29+
**Example 3:**
30+
31+
```
32+
Input: n = 5
33+
Output: 2
34+
Explanation: 5 in binary is "101".
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
ans, count := 0, 0
5+
first := true
6+
for ; n > 0; n >>= 1 {
7+
if n&1 == 0 {
8+
if !first {
9+
count++
10+
}
11+
continue
12+
}
13+
if first {
14+
first = false
15+
} else {
16+
ans = max(ans, count)
17+
}
18+
count = 1
19+
}
20+
return ans
521
}

leetcode/801-900/0868.Binary-Gap/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", 22, 2},
17+
{"TestCase2", 8, 0},
18+
{"TestCase3", 5, 2},
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)