当前位置:网站首页>696. count binary substring

696. count binary substring

2022-06-24 18:33:00 One star accompanies the moon

 Insert picture description here
This question belongs to one that can be , As soon as you do it, it will be useless
The length of the string is 5w, So we need to be linear or nlogn Time complexity of , But I used it at the very beginning n^2 Methods , direct TLE…
Normally speaking, it must take every point as the starting point , Look back , But then RE 了 (V-V)
Here is a good idea : We can count each paragraph 0(1) The length of , Then add up the smallest adjacent length . Take the example of the title 1 As an example, the length sequence of each segment is obtained :
[2,2,2,2]
that , The result is 2+2+2=6
Look at another set of data "00100":
[2,1,2]
that , The result is :1+1=2
What is the minimum length of two adjacent segments , Can be combined into how many xx01yy(yy10xx),0001111 For example , Its substring :000111 It's a situation , Substring :0011 It's also a situation ,01 It's also a situation , So there are three situations . Reduce the same amount each time 01, A new substring will be generated , So the shortest length is the number of substring occurrences

class Solution {
    
public:
    int countBinarySubstrings(string s) {
    
        int last=0,pre=0,len=s.size(),res=0,count,pos;
        while(pre<len){
    
            count=0;
            pos=pre;
            while(pre<len&&s[pre]==s[pos]){
    
                count++;
                pre++;
            }
            res+=min(count,last);
            last=count;
        }   
        return res;
    }
};
原网站

版权声明
本文为[One star accompanies the moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333580641.html