696. Count Binary Substrings
[思路]:
给一个0,1组成的字符串,计算连续0,1子串的个数,0,1个数相同;
- 首先找到0,1交界的地方,存起来;
- 然后在交界地方比较,左边和左边相等,右边和右边相等;
int countBinarySubstrings(string s) {
int n = s.length();
deque<int> d;
int sum=0;
for(int i=1;i<n;i++){
if(s[i-1] != s[i])
d.push_back(i);
}
while(!d.empty()){
int cur = d.front();
d.pop_front();
int left = cur-1;
sum++;
// cout<<"cur:"<<cur<<" sum: "<<sum<<endl;
while(left>0 && cur<n-1){
if(s[left] == s[left-1] && s[cur] == s[cur+1])
sum++;
else
break;
cur++;
left--;
// cout<<"cur:"<<cur<<" sum:"<<sum<<endl;
}
//cout<<"=========="<<endl;
}
return sum;
}