您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: