-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountBinarySubstrings.java
More file actions
24 lines (23 loc) · 714 Bytes
/
CountBinarySubstrings.java
File metadata and controls
24 lines (23 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package leetcode.string;
/**
* @ClassName CountBinarySubstrings
* @Description 计数二进制子串
* @Author changxuan
* @Date 2020/8/10 下午8:44
**/
public class CountBinarySubstrings {
public int countBinarySubstrings(String s) {
int[] counts = new int[s.length()];
char[] sChars = s.toCharArray();
int j = 0, result = 0; counts[j] = 1;
for (int i = 1; i < s.length(); i++){
if (sChars[i] == sChars[i-1]) counts[j]++;
else counts[++j]++;
}
for (int i = 1; i < counts.length; i++ ){
if (counts[i] == 0) break;
result = result + Math.min(counts[i-1], counts[i]);
}
return result;
}
}