Skip to content

Commit e362002

Browse files
authored
Merge pull request #1271 from 0xff-dev/1016
Add solution and test-cases for problem 1016
2 parents 12c3285 + a9f7a5b commit e362002

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [1016.Binary String With Substrings Representing 1 To N][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 binary string `s` and a positive integer `n`, return `true` if the binary representation of all the integers in the range `[1, n]` are **substrings** of `s`, or `false` otherwise.
5+
6+
A **substring** is a contiguous sequence of characters within a string.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s = "0110", n = 3
12+
Output: true
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Binary String With Substrings Representing 1 To N
23-
```go
2417
```
25-
18+
Input: s = "0110", n = 4
19+
Output: false
20+
```
2621

2722
## 结语
2823

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

3-
func Solution(x bool) bool {
4-
return x
3+
func pow(x int) int {
4+
base := 1
5+
6+
for i := 1; i <= x; i++ {
7+
base *= 2
8+
}
9+
return base
10+
}
11+
12+
func Solution(s string, n int) bool {
13+
count := make(map[int]struct{})
14+
ls := len(s)
15+
16+
loop := min(31, ls)
17+
for i := 1; i <= loop; i++ {
18+
base := 0
19+
for j := 0; j < i; j++ {
20+
base = base*2 + int(s[j]-'0')
21+
}
22+
if base != 0 && base <= n {
23+
count[base] = struct{}{}
24+
}
25+
b2 := pow(i - 1)
26+
start, end := 0, i
27+
for ; end < ls; start, end = start+1, end+1 {
28+
if s[start] == '1' {
29+
base -= b2
30+
}
31+
now := base << 1
32+
now += int(s[end] - '0')
33+
base = now
34+
if now == 0 || now > n {
35+
continue
36+
}
37+
count[now] = struct{}{}
38+
}
39+
}
40+
return len(count) == n
541
}

leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.n)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.n)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)