Leetcode696 Count Binary Substrings
class Solution {
public:
int countBinarySubstrings(string s) {
int result = 0;
int prev = 0;
int current = 1;
int n=s.size();
for(int i = 1 ; i < n ; i++){
if(s[i] == s[i-1]){
current++;
}else{
prev = current;
current = 1;
}
if(current <= prev){
result++;
}
}
return result;
}
};
体会
"连续"的联想:
(1)利用首尾/个数
(2)变化意味着突变