diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/README.md b/solution/1500-1599/1513.Number of Substrings With Only 1s/README.md index 12df5dee4a649..8759f21f26410 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/README.md +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/README.md @@ -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)$。 相似题目: @@ -91,14 +91,15 @@ 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 @@ -106,11 +107,15 @@ class Solution: ```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; } @@ -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; } @@ -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 } @@ -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 + } +} +``` + diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/README_EN.md b/solution/1500-1599/1513.Number of Substrings With Only 1s/README_EN.md index 6290f4df727b9..9e03ef0f7cb70 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/README_EN.md +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/README_EN.md @@ -62,7 +62,18 @@ tags: -### 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) @@ -71,14 +82,15 @@ 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 @@ -86,11 +98,15 @@ class Solution: ```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; } @@ -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; } @@ -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 } @@ -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 + } +} +``` + diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.cpp b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.cpp index f22e8a00d3b2a..95c645fdfdaf7 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.cpp +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.cpp @@ -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; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.go b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.go index a9e9026294cb6..591bd8b57f9b8 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.go +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.go @@ -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 -} \ No newline at end of file +} diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.java b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.java index 8fb716d14425c..e75b4a607c638 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.java +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.py b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.py index 8941ff531ea1d..20c98d238b7d1 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.py +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.py @@ -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 diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.rs b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.rs new file mode 100644 index 0000000000000..5909d588faa27 --- /dev/null +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.rs @@ -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 + } +} diff --git a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.ts b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.ts index 8261d1016156c..98bad41a03d50 100644 --- a/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.ts +++ b/solution/1500-1599/1513.Number of Substrings With Only 1s/Solution.ts @@ -1,10 +1,13 @@ 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; }