leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 83ac5443079442d4f30dfad82e1a03153148a423
parent cdf8e1a0e52b86fc55f104062637e45751f36a96
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sat, 15 Nov 2025 20:36:18 -0700

1513. number of substrings with only 1s

Diffstat:
Mnum_substrings_only_1.c | 17+++--------------
1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/num_substrings_only_1.c b/num_substrings_only_1.c @@ -7,21 +7,10 @@ */ int numSub(char *s) { - int n = strlen(s), mod = 1e9 + 7, ans = 0, one_cnt = 0; + int ans = 0, cnt = 0, mod = 1e9 + 7, n = strlen(s); for (int i = 0; i < n; i++) { - if (s[i] == '1') - one_cnt++; - else { - if (one_cnt) { - long long val = (long long)(one_cnt + 1) * one_cnt / 2; - ans = (ans + val % mod) % mod; - } - one_cnt = 0; - } - } - if (one_cnt) { - long long val = (long long)(one_cnt + 1) * one_cnt / 2; - ans = (ans + val % mod) % mod; + cnt = s[i] == '1' ? cnt + 1 : 0; + ans = (ans + cnt) % mod; } return ans; }