Skip to content

Commit c49db85

Browse files
authored
Merge pull request #1207 from 0xff-dev/1513
Add solution and test-cases for problem 1513
2 parents d08521e + d057ae0 commit c49db85

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [1513.Number of Substrings With Only 1s][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`, return the number of substrings with all characters `1`'s. Since the answer may be too large, return it modulo `10^9 + 7`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: s = "0110111"
10+
Output: 9
11+
Explanation: There are 9 substring in total with only 1's characters.
12+
"1" -> 5 times.
13+
"11" -> 3 times.
14+
"111" -> 1 time.
1315
```
1416

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

20-
### 思路1
21-
> ...
22-
Number of Substrings With Only 1s
23-
```go
19+
```
20+
Input: s = "101"
21+
Output: 2
22+
Explanation: Substring "1" is shown 2 times in s.
2423
```
2524

25+
**Example 3:**
26+
27+
```
28+
Input: s = "111111"
29+
Output: 21
30+
Explanation: Each substring contains only 1's characters.
31+
```
2632

2733
## 结语
2834

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod1513 = 1e9 + 7
4+
5+
func Solution(s string) int {
6+
one := 0
7+
ans := 0
8+
for _, b := range s {
9+
if b == '0' {
10+
// 1111
11+
// 4 3 2 1
12+
ans = (ans + one*(one+1)/2) % mod1513
13+
one = 0
14+
continue
15+
}
16+
one++
17+
}
18+
ans = (ans + one*(one+1)/2) % mod1513
19+
return ans
520
}

leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "0110111", 9},
17+
{"TestCase2", "101", 2},
18+
{"TestCase3", "111111", 21},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)