696. Count Binary Substrings
2020-01-14 09:32
169 查看
#696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
这道题的意思是,找出字符串中等个数的连续的0,1
这道题的思路是,判断前一个和当前是否一样,一样的话再看是不是小于之前的不同数字数量,是的话就+1。如果前一个和当前不一样,就把当前置为1。之前的数字数量置为前一个。
class Solution { public int countBinarySubstrings(String s) { int res = 0; int prev = 0; int cur = 1; for(int i=1;i<s.length();i++) { if(s.charAt(i) == s.charAt(i-1)) { cur++; } else { prev = cur; cur = 1; } if(prev>=cur) { res++; } } return res; } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- leetcode 696 Count Binary Substrings
- LeetCode-696-Count Binary Substringss-E
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- Leetcode 696 Count Binary Substrings
- 5行代码怎么实现Hadoop的WordCount?
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- max_prepared_stmt_count
- Count++不是线程安全的 不是原子性的
- Hadoop2.5.1在Ubuntu14中的伪分布配置+wordcount测试+eclipse环境配置
- bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
- HDU 4588 Count The Carries (2013年南京邀请赛)
- Leetcode 357 Count Numbers with Unique Digits
- SQL Server中count(*), count(col), count(1)的对比
- wordcount-使用eclipse插件中对文件路径的修改
- count and say