696. Count Binary Substrings
2018-01-10 14:43
190 查看
题目描述:
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.
思路一:
class Solution {
public int countBinarySubstrings(String s) {
if (s == null || s.length() == 0)
return 0;
int cnt = 0;
int preLength = 0;
int currLength = 1;
for (int i = 1; i < s.length(); i++)
{
if (s.charAt(i) == s.charAt(i - 1))
currLength++;
else
{
preLength = currLength;
currLength = 1;
}
if (preLength >= currLength)
cnt++;
}
return cnt;
}
}
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.
思路一:
class Solution {
public int countBinarySubstrings(String s) {
if (s == null || s.length() == 0)
return 0;
int cnt = 0;
int preLength = 0;
int currLength = 1;
for (int i = 1; i < s.length(); i++)
{
if (s.charAt(i) == s.charAt(i - 1))
currLength++;
else
{
preLength = currLength;
currLength = 1;
}
if (preLength >= currLength)
cnt++;
}
return cnt;
}
}
相关文章推荐
- leetcode 696 Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- Leetcode 696 Count Binary Substrings
- 696. Count Binary Substrings
- 696. Count Binary Substrings
- LeetCode-696-Count Binary Substringss-E
- oracle count计数的优化
- NSMutableArray类addObject方法对对象retainCount的影响 --在MRC下
- Mysql中count(*),DISTINCT的使用方法和效率研究
- sqlserver 关于count(1),count(*)测试
- 关于sql中的count
- MySQL查询优化之COUNT()
- 详测 Generics Collections TList (1): Add、Clear、Count、Capacity
- leetcode——Count Primes
- ORACLE中的SQL%ROWCOUNT与MySQL中的ROW_COUNT()的一点异同
- leetcode_Count and Say
- 存储过程中的 SET NOCOUNT ON
- 查看jar里面到底什么鬼~(hadoop学习wordcount程序代码查看)