classSolution {
public:int numSub(string s) {
constint MOD =1e9+7;
long ans =0, cnt =0;
for (char c : s) {
if (c =='1') cnt++;
else { ans += cnt * (cnt +1) /2; cnt =0; }
}
ans += cnt * (cnt +1) /2;
return ans % MOD;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcnumSub(sstring) int {
mod:= int(1e9+7)
ans, cnt:=0, 0for_, c:=ranges {
ifc=='1' {
cnt++ } else {
ans = (ans+cnt*(cnt+1)/2) %modcnt = 0 }
}
ans = (ans+cnt*(cnt+1)/2) %modreturnans}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
publicintnumSub(String s) {
int MOD = 1_000_000_007;
long ans = 0, cnt = 0;
for (char c : s.toCharArray()) {
if (c =='1') cnt++;
else { ans += cnt * (cnt + 1) / 2; cnt = 0; }
}
ans += cnt * (cnt + 1) / 2;
return (int)(ans % MOD);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funnumSub(s: String): Int {
val MOD = 1_000_000_007
var ans = 0Lvar cnt = 0Lfor (c in s) {
if (c =='1') cnt++else { ans = (ans + cnt * (cnt + 1) / 2) % MOD; cnt = 0 }
}
ans = (ans + cnt * (cnt + 1) / 2) % MOD
return ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defnumSub(self, s: str) -> int:
MOD =10**9+7 ans = cnt =0for c in s:
if c =='1':
cnt +=1else:
ans += cnt * (cnt +1) //2 cnt =0 ans += cnt * (cnt +1) //2return ans % MOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnnum_sub(s: String) -> i32 {
let m =1_000_000_007;
letmut ans =0i64;
letmut cnt =0i64;
for c in s.chars() {
if c =='1' {
cnt +=1;
} else {
ans = (ans + cnt * (cnt +1) /2) % m;
cnt =0;
}
}
ans = (ans + cnt * (cnt +1) /2) % m;
ans asi32 }
}