Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions leetcode/1501-1600/1513.Number-of-Substrings-With-Only-1s/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading