696. Count Binary Substrings
2018-01-15 10:10
344 查看
统计1的个数小于等于0的个数,可能为”01”,”001”,”00011”,存在符合条件的字符串
统计0的个数小于等于1的个数,可能为”10”,”110”,”11100”。
int countBinarySubstrings(string s) { int zeros=0,ones=0,res=0; for(int i=0;i<s.size();i++){ if(i==0){ (s[i]=='1')?++ones:++zeros; }else{ if(s[i]=='1'){ ones=(s[i-1]=='1')?ones+1:1; if(zeros>=ones) ++res; }else if(s[i]=='0'){ zeros=(s[i-1]=='0')?zeros+1:1; if(ones>=zeros) ++res; } } } return res; }
int countBinarySubstrings(string s) { int res=0,pre=0,cur=1,n=s.size(); for(int i=1;i<n;i++){ if(s[i]==s[i-1]) ++cur; else{ pre=cur; cur=1; } if(pre>=cur) ++res; } return res; }
相关文章推荐
- 696. Count Binary Substrings
- Leetcode 696 Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- LeetCode-696-Count Binary Substringss-E
- leetcode 696 Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 条款45:注意count、find、binary_search、lower_bound、upper_bound
- <leetcode系列> Count And Say
- STL algorithm之count、find、binary_search、lower_bound、upper_bound和equal_range的区别
- countRectangles
- 非变异算法之计数(count, count_if)
- hdu 5471 Count the Grid(容斥)
- ITPUB: HAVING COUNT 的妙用
- LeetCode OJ:Count Primes(质数计数)
- SQL SET NOCOUNT ON的含义和作用
- Incorrect parameter count in the call to native function 'ISNULL'
- PHP学习笔记1 数组函数 array range array_pad array_splice count
- Count and Say