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
89 changes: 61 additions & 28 deletions solution/1500-1599/1513.Number of Substrings With Only 1s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ tags:

### 方法一:遍历计数

我们遍历字符串 $s$,用变量 $cnt$ 记录当前连续的 1 的个数,用变量 $ans$ 记录答案。当遍历到字符 $s[i]$ 时,如果 $s[i] = 1$,则 $cnt$ 自增 1,否则 $cnt$ 置 0。此时 $ans$ 自增 $cnt$
我们遍历字符串 $s$,用一个变量 $\textit{cur}$ 记录当前连续的 1 的个数,用变量 $\textit{ans}$ 记录答案。当遍历到字符 $s[i]$ 时,如果 $s[i] = 0$,则 $\textit{cur}$ 置 0,否则 $\textit{cur}$ 自增 1,然后 $\textit{ans}$ 自增 $\textit{cur}$,并对 $10^9 + 7$ 取模

遍历结束,返回 $ans$ 即可。
遍历结束,返回 $\textit{ans}$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$

相似题目:

Expand All @@ -91,26 +91,31 @@ tags:
```python
class Solution:
def numSub(self, s: str) -> int:
ans = cnt = 0
mod = 10**9 + 7
ans = cur = 0
for c in s:
if c == "1":
cnt += 1
if c == "0":
cur = 0
else:
cnt = 0
ans += cnt
return ans % (10**9 + 7)
cur += 1
ans = (ans + cur) % mod
return ans
```

#### Java

```java
class Solution {
public int numSub(String s) {
final int mod = (int) 1e9 + 7;
int ans = 0, cnt = 0;
final int mod = 1_000_000_007;
int ans = 0, cur = 0;
for (int i = 0; i < s.length(); ++i) {
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
if (s.charAt(i) == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
Expand All @@ -123,11 +128,15 @@ class Solution {
class Solution {
public:
int numSub(string s) {
int ans = 0, cnt = 0;
const int mod = 1e9 + 7;
for (char& c : s) {
cnt = c == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
int ans = 0, cur = 0;
for (char c : s) {
if (c == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
Expand All @@ -138,15 +147,15 @@ public:

```go
func numSub(s string) (ans int) {
const mod = 1e9 + 7
cnt := 0
const mod int = 1e9 + 7
cur := 0
for _, c := range s {
if c == '1' {
cnt++
if c == '0' {
cur = 0
} else {
cnt = 0
cur++
ans = (ans + cur) % mod
}
ans = (ans + cnt) % mod
}
return
}
Expand All @@ -156,17 +165,41 @@ func numSub(s string) (ans int) {

```ts
function numSub(s: string): number {
const mod = 10 ** 9 + 7;
let ans = 0;
let cnt = 0;
const mod = 1_000_000_007;
let [ans, cur] = [0, 0];
for (const c of s) {
cnt = c == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
if (c === '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn num_sub(s: String) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut ans: i32 = 0;
let mut cur: i32 = 0;
for c in s.chars() {
if c == '0' {
cur = 0;
} else {
cur += 1;
ans = (ans + cur) % MOD;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal and Counting

We traverse the string $s$, using a variable $\textit{cur}$ to record the current count of consecutive 1s, and a variable $\textit{ans}$ to record the answer. When we traverse to character $s[i]$, if $s[i] = 0$, then set $\textit{cur}$ to 0; otherwise, increment $\textit{cur}$ by 1, then add $\textit{cur}$ to $\textit{ans}$, and take modulo $10^9 + 7$.

After the traversal is complete, return $\textit{ans}$.

The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.

Similar problems:

- [413. Arithmetic Slices](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md)
- [2348. Number of Zero-Filled Subarrays](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2348.Number%20of%20Zero-Filled%20Subarrays/README_EN.md)

<!-- tabs:start -->

Expand All @@ -71,26 +82,31 @@ tags:
```python
class Solution:
def numSub(self, s: str) -> int:
ans = cnt = 0
mod = 10**9 + 7
ans = cur = 0
for c in s:
if c == "1":
cnt += 1
if c == "0":
cur = 0
else:
cnt = 0
ans += cnt
return ans % (10**9 + 7)
cur += 1
ans = (ans + cur) % mod
return ans
```

#### Java

```java
class Solution {
public int numSub(String s) {
final int mod = (int) 1e9 + 7;
int ans = 0, cnt = 0;
final int mod = 1_000_000_007;
int ans = 0, cur = 0;
for (int i = 0; i < s.length(); ++i) {
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
if (s.charAt(i) == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
Expand All @@ -103,11 +119,15 @@ class Solution {
class Solution {
public:
int numSub(string s) {
int ans = 0, cnt = 0;
const int mod = 1e9 + 7;
for (char& c : s) {
cnt = c == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
int ans = 0, cur = 0;
for (char c : s) {
if (c == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
Expand All @@ -118,15 +138,15 @@ public:

```go
func numSub(s string) (ans int) {
const mod = 1e9 + 7
cnt := 0
const mod int = 1e9 + 7
cur := 0
for _, c := range s {
if c == '1' {
cnt++
if c == '0' {
cur = 0
} else {
cnt = 0
cur++
ans = (ans + cur) % mod
}
ans = (ans + cnt) % mod
}
return
}
Expand All @@ -136,17 +156,41 @@ func numSub(s string) (ans int) {

```ts
function numSub(s: string): number {
const mod = 10 ** 9 + 7;
let ans = 0;
let cnt = 0;
const mod = 1_000_000_007;
let [ans, cur] = [0, 0];
for (const c of s) {
cnt = c == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
if (c === '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn num_sub(s: String) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut ans: i32 = 0;
let mut cur: i32 = 0;
for c in s.chars() {
if c == '0' {
cur = 0;
} else {
cur += 1;
ans = (ans + cur) % MOD;
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class Solution {
public:
int numSub(string s) {
int ans = 0, cnt = 0;
const int mod = 1e9 + 7;
for (char& c : s) {
cnt = c == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
int ans = 0, cur = 0;
for (char c : s) {
if (c == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
func numSub(s string) (ans int) {
const mod = 1e9 + 7
cnt := 0
const mod int = 1e9 + 7
cur := 0
for _, c := range s {
if c == '1' {
cnt++
if c == '0' {
cur = 0
} else {
cnt = 0
cur++
ans = (ans + cur) % mod
}
ans = (ans + cnt) % mod
}
return
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class Solution {
public int numSub(String s) {
final int mod = (int) 1e9 + 7;
int ans = 0, cnt = 0;
final int mod = 1_000_000_007;
int ans = 0, cur = 0;
for (int i = 0; i < s.length(); ++i) {
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
ans = (ans + cnt) % mod;
if (s.charAt(i) == '0') {
cur = 0;
} else {
cur++;
ans = (ans + cur) % mod;
}
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Solution:
def numSub(self, s: str) -> int:
ans = cnt = 0
mod = 10**9 + 7
ans = cur = 0
for c in s:
if c == "1":
cnt += 1
if c == "0":
cur = 0
else:
cnt = 0
ans += cnt
return ans % (10**9 + 7)
cur += 1
ans = (ans + cur) % mod
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
impl Solution {
pub fn num_sub(s: String) -> i32 {
const MOD: i32 = 1_000_000_007;
let mut ans: i32 = 0;
let mut cur: i32 = 0;
for c in s.chars() {
if c == '0' {
cur = 0;
} else {
cur += 1;
ans = (ans + cur) % MOD;
}
}
ans
}
}
Loading
Loading