diff --git a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/README.md b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/README.md index 873cf1a45..169771c3c 100755 --- a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/README.md +++ b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/README.md @@ -1,28 +1,35 @@ # [3211.Generate Binary Strings Without Adjacent Zeros][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a positive integer `n`. + +A binary string `x` is **valid** if all substrings of `x` of length 2 contain **at least** one `"1"`. + +Return all **valid** strings with length `n`, in any order. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: n = 3 + +Output: ["010","011","101","110","111"] -## 题意 -> ... +Explanation: -## 题解 +The valid strings of length 3 are: "010", "011", "101", "110", and "111". +``` + +**Example 2:** -### 思路1 -> ... -Generate Binary Strings Without Adjacent Zeros -```go ``` +Input: n = 1 + +Output: ["0","1"] +Explanation: + +The valid strings of length 1 are: "0" and "1". +``` ## 结语 diff --git a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution.go b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution.go index d115ccf5e..8e24f5421 100644 --- a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution.go +++ b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int) []string { + var res []string + var dfs func(int, string, bool) + dfs = func(index int, cur string, isZero bool) { + if index == n { + res = append(res, cur) + return + } + if !isZero { + dfs(index+1, cur+"0", true) + } + dfs(index+1, cur+"1", false) + } + dfs(0, "", false) + return res } diff --git a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution_test.go b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution_test.go index 14ff50eb4..b5e23cc8f 100644 --- a/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution_test.go +++ b/leetcode/3201-3300/3211.Generate-Binary-Strings-Without-Adjacent-Zeros/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, []string{"010", "011", "101", "110", "111"}}, + {"TestCase2", 1, []string{"0", "1"}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }