Skip to content

Commit 6ca1e82

Browse files
authored
Merge pull request #1190 from 0xff-dev/3211
Add solution and test-cases for problem 3211
2 parents 2cc0c02 + 64f710d commit 6ca1e82

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [3211.Generate Binary Strings Without Adjacent Zeros][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 integer `n`.
5+
6+
A binary string `x` is **valid** if all substrings of `x` of length 2 contain **at least** one `"1"`.
7+
8+
Return all **valid** strings with length `n`, in any order.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
Input: n = 3
14+
15+
Output: ["010","011","101","110","111"]
1416
15-
## 题意
16-
> ...
17+
Explanation:
1718
18-
## 题解
19+
The valid strings of length 3 are: "010", "011", "101", "110", and "111".
20+
```
21+
22+
**Example 2:**
1923

20-
### 思路1
21-
> ...
22-
Generate Binary Strings Without Adjacent Zeros
23-
```go
2424
```
25+
Input: n = 1
26+
27+
Output: ["0","1"]
2528
29+
Explanation:
30+
31+
The valid strings of length 1 are: "0" and "1".
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) []string {
4+
var res []string
5+
var dfs func(int, string, bool)
6+
dfs = func(index int, cur string, isZero bool) {
7+
if index == n {
8+
res = append(res, cur)
9+
return
10+
}
11+
if !isZero {
12+
dfs(index+1, cur+"0", true)
13+
}
14+
dfs(index+1, cur+"1", false)
15+
}
16+
dfs(0, "", false)
17+
return res
518
}

leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, []string{"010", "011", "101", "110", "111"}},
17+
{"TestCase2", 1, []string{"0", "1"}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)