-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountConsecutiveOnes.java
45 lines (42 loc) · 1.12 KB
/
CountConsecutiveOnes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*https://leetcode.com/problems/number-of-substrings-with-only-1s/*/
class Solution {
public int numSub(String s) {
long consecutiveCount = 0, result = 0, mod = 1000000007;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '0')
{
result += ((consecutiveCount*(consecutiveCount+1))/2)%mod;
result %= mod;
consecutiveCount = 0;
}
else
{
++consecutiveCount;
}
}
if (s.charAt(s.length()-1) == '1')
{
result += ((consecutiveCount*(consecutiveCount+1))/2)%mod;
result %= mod;
}
return (int)(result%mod);
}
}
class Solution {
public int numSub(String s) {
char[] ch = s.toCharArray();
long count =0;
long result =0;
for(int i=0; i<ch.length; i++)
{
if(ch[i] == '1')
{
count++;
result += count;
}
else
count = 0;
}
return (int)(result%1000000007);
}
}