Skip to content

Commit 126c576

Browse files
authored
feat: add solutions to lc problem: No.1513 (#4837)
1 parent d2c1447 commit 126c576

File tree

8 files changed

+187
-82
lines changed

8 files changed

+187
-82
lines changed

solution/1500-1599/1513.Number of Substrings With Only 1s/README.md

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ tags:
7373

7474
### 方法一:遍历计数
7575

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

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

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

8282
相似题目:
8383

@@ -91,26 +91,31 @@ tags:
9191
```python
9292
class Solution:
9393
def numSub(self, s: str) -> int:
94-
ans = cnt = 0
94+
mod = 10**9 + 7
95+
ans = cur = 0
9596
for c in s:
96-
if c == "1":
97-
cnt += 1
97+
if c == "0":
98+
cur = 0
9899
else:
99-
cnt = 0
100-
ans += cnt
101-
return ans % (10**9 + 7)
100+
cur += 1
101+
ans = (ans + cur) % mod
102+
return ans
102103
```
103104

104105
#### Java
105106

106107
```java
107108
class Solution {
108109
public int numSub(String s) {
109-
final int mod = (int) 1e9 + 7;
110-
int ans = 0, cnt = 0;
110+
final int mod = 1_000_000_007;
111+
int ans = 0, cur = 0;
111112
for (int i = 0; i < s.length(); ++i) {
112-
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
113-
ans = (ans + cnt) % mod;
113+
if (s.charAt(i) == '0') {
114+
cur = 0;
115+
} else {
116+
cur++;
117+
ans = (ans + cur) % mod;
118+
}
114119
}
115120
return ans;
116121
}
@@ -123,11 +128,15 @@ class Solution {
123128
class Solution {
124129
public:
125130
int numSub(string s) {
126-
int ans = 0, cnt = 0;
127131
const int mod = 1e9 + 7;
128-
for (char& c : s) {
129-
cnt = c == '1' ? cnt + 1 : 0;
130-
ans = (ans + cnt) % mod;
132+
int ans = 0, cur = 0;
133+
for (char c : s) {
134+
if (c == '0') {
135+
cur = 0;
136+
} else {
137+
cur++;
138+
ans = (ans + cur) % mod;
139+
}
131140
}
132141
return ans;
133142
}
@@ -138,15 +147,15 @@ public:
138147
139148
```go
140149
func numSub(s string) (ans int) {
141-
const mod = 1e9 + 7
142-
cnt := 0
150+
const mod int = 1e9 + 7
151+
cur := 0
143152
for _, c := range s {
144-
if c == '1' {
145-
cnt++
153+
if c == '0' {
154+
cur = 0
146155
} else {
147-
cnt = 0
156+
cur++
157+
ans = (ans + cur) % mod
148158
}
149-
ans = (ans + cnt) % mod
150159
}
151160
return
152161
}
@@ -156,17 +165,41 @@ func numSub(s string) (ans int) {
156165

157166
```ts
158167
function numSub(s: string): number {
159-
const mod = 10 ** 9 + 7;
160-
let ans = 0;
161-
let cnt = 0;
168+
const mod = 1_000_000_007;
169+
let [ans, cur] = [0, 0];
162170
for (const c of s) {
163-
cnt = c == '1' ? cnt + 1 : 0;
164-
ans = (ans + cnt) % mod;
171+
if (c === '0') {
172+
cur = 0;
173+
} else {
174+
cur++;
175+
ans = (ans + cur) % mod;
176+
}
165177
}
166178
return ans;
167179
}
168180
```
169181

182+
#### Rust
183+
184+
```rust
185+
impl Solution {
186+
pub fn num_sub(s: String) -> i32 {
187+
const MOD: i32 = 1_000_000_007;
188+
let mut ans: i32 = 0;
189+
let mut cur: i32 = 0;
190+
for c in s.chars() {
191+
if c == '0' {
192+
cur = 0;
193+
} else {
194+
cur += 1;
195+
ans = (ans + cur) % MOD;
196+
}
197+
}
198+
ans
199+
}
200+
}
201+
```
202+
170203
<!-- tabs:end -->
171204

172205
<!-- solution:end -->

solution/1500-1599/1513.Number of Substrings With Only 1s/README_EN.md

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Traversal and Counting
66+
67+
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$.
68+
69+
After the traversal is complete, return $\textit{ans}$.
70+
71+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
72+
73+
Similar problems:
74+
75+
- [413. Arithmetic Slices](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md)
76+
- [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)
6677

6778
<!-- tabs:start -->
6879

@@ -71,26 +82,31 @@ tags:
7182
```python
7283
class Solution:
7384
def numSub(self, s: str) -> int:
74-
ans = cnt = 0
85+
mod = 10**9 + 7
86+
ans = cur = 0
7587
for c in s:
76-
if c == "1":
77-
cnt += 1
88+
if c == "0":
89+
cur = 0
7890
else:
79-
cnt = 0
80-
ans += cnt
81-
return ans % (10**9 + 7)
91+
cur += 1
92+
ans = (ans + cur) % mod
93+
return ans
8294
```
8395

8496
#### Java
8597

8698
```java
8799
class Solution {
88100
public int numSub(String s) {
89-
final int mod = (int) 1e9 + 7;
90-
int ans = 0, cnt = 0;
101+
final int mod = 1_000_000_007;
102+
int ans = 0, cur = 0;
91103
for (int i = 0; i < s.length(); ++i) {
92-
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
93-
ans = (ans + cnt) % mod;
104+
if (s.charAt(i) == '0') {
105+
cur = 0;
106+
} else {
107+
cur++;
108+
ans = (ans + cur) % mod;
109+
}
94110
}
95111
return ans;
96112
}
@@ -103,11 +119,15 @@ class Solution {
103119
class Solution {
104120
public:
105121
int numSub(string s) {
106-
int ans = 0, cnt = 0;
107122
const int mod = 1e9 + 7;
108-
for (char& c : s) {
109-
cnt = c == '1' ? cnt + 1 : 0;
110-
ans = (ans + cnt) % mod;
123+
int ans = 0, cur = 0;
124+
for (char c : s) {
125+
if (c == '0') {
126+
cur = 0;
127+
} else {
128+
cur++;
129+
ans = (ans + cur) % mod;
130+
}
111131
}
112132
return ans;
113133
}
@@ -118,15 +138,15 @@ public:
118138
119139
```go
120140
func numSub(s string) (ans int) {
121-
const mod = 1e9 + 7
122-
cnt := 0
141+
const mod int = 1e9 + 7
142+
cur := 0
123143
for _, c := range s {
124-
if c == '1' {
125-
cnt++
144+
if c == '0' {
145+
cur = 0
126146
} else {
127-
cnt = 0
147+
cur++
148+
ans = (ans + cur) % mod
128149
}
129-
ans = (ans + cnt) % mod
130150
}
131151
return
132152
}
@@ -136,17 +156,41 @@ func numSub(s string) (ans int) {
136156

137157
```ts
138158
function numSub(s: string): number {
139-
const mod = 10 ** 9 + 7;
140-
let ans = 0;
141-
let cnt = 0;
159+
const mod = 1_000_000_007;
160+
let [ans, cur] = [0, 0];
142161
for (const c of s) {
143-
cnt = c == '1' ? cnt + 1 : 0;
144-
ans = (ans + cnt) % mod;
162+
if (c === '0') {
163+
cur = 0;
164+
} else {
165+
cur++;
166+
ans = (ans + cur) % mod;
167+
}
145168
}
146169
return ans;
147170
}
148171
```
149172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn num_sub(s: String) -> i32 {
178+
const MOD: i32 = 1_000_000_007;
179+
let mut ans: i32 = 0;
180+
let mut cur: i32 = 0;
181+
for c in s.chars() {
182+
if c == '0' {
183+
cur = 0;
184+
} else {
185+
cur += 1;
186+
ans = (ans + cur) % MOD;
187+
}
188+
}
189+
ans
190+
}
191+
}
192+
```
193+
150194
<!-- tabs:end -->
151195

152196
<!-- solution:end -->
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
class Solution {
22
public:
33
int numSub(string s) {
4-
int ans = 0, cnt = 0;
54
const int mod = 1e9 + 7;
6-
for (char& c : s) {
7-
cnt = c == '1' ? cnt + 1 : 0;
8-
ans = (ans + cnt) % mod;
5+
int ans = 0, cur = 0;
6+
for (char c : s) {
7+
if (c == '0') {
8+
cur = 0;
9+
} else {
10+
cur++;
11+
ans = (ans + cur) % mod;
12+
}
913
}
1014
return ans;
1115
}
12-
};
16+
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
func numSub(s string) (ans int) {
2-
const mod = 1e9 + 7
3-
cnt := 0
2+
const mod int = 1e9 + 7
3+
cur := 0
44
for _, c := range s {
5-
if c == '1' {
6-
cnt++
5+
if c == '0' {
6+
cur = 0
77
} else {
8-
cnt = 0
8+
cur++
9+
ans = (ans + cur) % mod
910
}
10-
ans = (ans + cnt) % mod
1111
}
1212
return
13-
}
13+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
class Solution {
22
public int numSub(String s) {
3-
final int mod = (int) 1e9 + 7;
4-
int ans = 0, cnt = 0;
3+
final int mod = 1_000_000_007;
4+
int ans = 0, cur = 0;
55
for (int i = 0; i < s.length(); ++i) {
6-
cnt = s.charAt(i) == '1' ? cnt + 1 : 0;
7-
ans = (ans + cnt) % mod;
6+
if (s.charAt(i) == '0') {
7+
cur = 0;
8+
} else {
9+
cur++;
10+
ans = (ans + cur) % mod;
11+
}
812
}
913
return ans;
1014
}
11-
}
15+
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution:
22
def numSub(self, s: str) -> int:
3-
ans = cnt = 0
3+
mod = 10**9 + 7
4+
ans = cur = 0
45
for c in s:
5-
if c == "1":
6-
cnt += 1
6+
if c == "0":
7+
cur = 0
78
else:
8-
cnt = 0
9-
ans += cnt
10-
return ans % (10**9 + 7)
9+
cur += 1
10+
ans = (ans + cur) % mod
11+
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn num_sub(s: String) -> i32 {
3+
const MOD: i32 = 1_000_000_007;
4+
let mut ans: i32 = 0;
5+
let mut cur: i32 = 0;
6+
for c in s.chars() {
7+
if c == '0' {
8+
cur = 0;
9+
} else {
10+
cur += 1;
11+
ans = (ans + cur) % MOD;
12+
}
13+
}
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)