1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 int countBinarySubstrings(string s) 12 { 13 int count=0; 14 int pre=0,cur=1; 15 int len=s.length(); 16 for(int i=1;i<len;i++) 17 { 18 if(s[i]==s[i-1]) 19 cur++; 20 else 21 { 22 count+=min(pre,cur); 23 pre=cur; 24 cur=1; 25 } 26 } 27 return count+min(pre,cur); 28 } 29 };
简单快捷
本文介绍了一种计算字符串中连续相同字符形成的二进制子串数量的方法。通过遍历字符串并跟踪当前和前一组相同字符的数量,利用最小值累加的方式高效地计算出了所有相邻且等长的0和1子串的数量。
358

被折叠的 条评论
为什么被折叠?



