方法一:遍历所有子串(超时)
方法二:统计所有连续字符的数量
class Solution {
public int countBinarySubstrings(String s) {
List<Integer> list = new ArrayList<Integer>();
int left = 0;
int sum = 0;
int right = s.length();
while(left<right){//双指针
char str = s.charAt(left);
int count = 0;
while(left<right&&s.charAt(left)==str){
count++;
left++;
}
list.add(count);
}
for(int i=1;i<list.size();i++){
sum = sum + Math.min(list.get(i-1), list.get(i));
}
return sum;
}
}