diff --git a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/README.md b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/README.md index f75579506..96df30725 100644 --- a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/README.md +++ b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/README.md @@ -1,28 +1,23 @@ # [1016.Binary String With Substrings Representing 1 To N][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 +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. + +A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "0110", n = 3 +Output: true ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Binary String With Substrings Representing 1 To N -```go ``` - +Input: s = "0110", n = 4 +Output: false +``` ## 结语 diff --git a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution.go b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution.go index d115ccf5e..6bc650cad 100644 --- a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution.go +++ b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution.go @@ -1,5 +1,41 @@ package Solution -func Solution(x bool) bool { - return x +func pow(x int) int { + base := 1 + + for i := 1; i <= x; i++ { + base *= 2 + } + return base +} + +func Solution(s string, n int) bool { + count := make(map[int]struct{}) + ls := len(s) + + loop := min(31, ls) + for i := 1; i <= loop; i++ { + base := 0 + for j := 0; j < i; j++ { + base = base*2 + int(s[j]-'0') + } + if base != 0 && base <= n { + count[base] = struct{}{} + } + b2 := pow(i - 1) + start, end := 0, i + for ; end < ls; start, end = start+1, end+1 { + if s[start] == '1' { + base -= b2 + } + now := base << 1 + now += int(s[end] - '0') + base = now + if now == 0 || now > n { + continue + } + count[now] = struct{}{} + } + } + return len(count) == n } diff --git a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution_test.go b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution_test.go index 14ff50eb4..ed548c9aa 100644 --- a/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution_test.go +++ b/leetcode/1001-1100/1016.Binary-String-With-Substrings-Representing-1-To-N/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string + n int expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "0110", 3, true}, + {"TestCase2", "0110", 4, false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.n) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.n) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }