From d057ae02ede133f31b5f297ba3fefd83b8225864 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 17 May 2025 16:32:37 +0800 Subject: [PATCH] Add solution and test-cases for problem 1513 --- .../README.md | 32 +++++++++++-------- .../Solution.go | 19 +++++++++-- .../Solution_test.go | 14 ++++---- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md index 56280fa2f..3c3234674 100755 --- a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md +++ b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md @@ -1,28 +1,34 @@ # [1513.Number of Substrings With Only 1s][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`, return the number of substrings with all characters `1`'s. Since the answer may be too large, return it modulo `10^9 + 7`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "0110111" +Output: 9 +Explanation: There are 9 substring in total with only 1's characters. +"1" -> 5 times. +"11" -> 3 times. +"111" -> 1 time. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Number of Substrings With Only 1s -```go +``` +Input: s = "101" +Output: 2 +Explanation: Substring "1" is shown 2 times in s. ``` +**Example 3:** + +``` +Input: s = "111111" +Output: 21 +Explanation: Each substring contains only 1's characters. +``` ## 结语 diff --git a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution.go b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution.go index d115ccf5e..986102eb6 100644 --- a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution.go +++ b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +const mod1513 = 1e9 + 7 + +func Solution(s string) int { + one := 0 + ans := 0 + for _, b := range s { + if b == '0' { + // 1111 + // 4 3 2 1 + ans = (ans + one*(one+1)/2) % mod1513 + one = 0 + continue + } + one++ + } + ans = (ans + one*(one+1)/2) % mod1513 + return ans } diff --git a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution_test.go b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution_test.go index 14ff50eb4..c4dbe2080 100644 --- a/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution_test.go +++ b/leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "0110111", 9}, + {"TestCase2", "101", 2}, + {"TestCase3", "111111", 21}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }