给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的。

重复出现的子串要计算它们出现的次数。

示例 1 :

输入: "00110011" 输出: 6 解释: 有6个子串具有相同数量的连续1和0:“0011”,“01”,“1100”,“10”,“0011” 和 “01”。 请注意,一些重复出现的子串要计算它们出现的次数。 另外,“00110011”不是有效的子串,因为所有的0(和1)没有组合在一起。

示例 2 :

输入: "10101" 输出: 4 解释: 有4个子串:“10”,“01”,“10”,“01”,它们具有相同数量的连续1和0。

注意:

  • s.length 在1到50,000之间。
  • s 只包含“0”或“1”字符。
class Solution {
public:
int countBinarySubstrings(string s) {
int len = s.size();
if(len == 0)
return 0;
int flag = -1;
int lastcnt = -1;
int cnt = 0;
int res = 0;
for(int i = 0; i < len; i++)
{
if(flag == -1)
{
flag = s[i] - '0';
cnt++;
continue;
}
else
{
if(flag == s[i] - '0')
{
cnt++;
if(cnt <= lastcnt)
res++;
continue;
}
else
{
lastcnt = cnt;
cnt = 1;
flag = flag ^ 1;
if(cnt <= lastcnt)
res++;
}
}
}
return res;
}
};

Leetcode696.Count Binary Substrings计算二进制字串的更多相关文章

  1. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  2. 【Leetcode_easy】696. Count Binary Substrings

    problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...

  3. 696. Count Binary Substrings - LeetCode

    Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...

  4. [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  5. HDU 4588 Count The Carries 计算二进制进位总数

    点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  6. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  7. LeetCode 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  8. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  9. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

随机推荐

  1. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  2. MacOS 读写 NTFS 即插即用.

    1. 安装osxfusehttps://osxfuse.github.io/ 2. 安装brewhttps://brew.sh/index_zh-cn.html 3. 安装ntfs-3gbrew in ...

  3. jquery的each()遍历和ajax传值

    页面展示 JS代码部分 /*功能:删除选中用户信息数据*/ function delUser(){ $("#delU").click(function(){ var unoStr ...

  4. chage 修改用户密码有效期限的命令

    chage 用语法格式: chage [-l] [-m 最小天数] [-M 最大天数] [-W 警告] [-I 失效日] [-E 过期日] [-d 最后日] 用户 前面已经说的好多了,这个只是一笔带过 ...

  5. vue elementui点击表格当前行radio单选选中

    官方文档:https://element.eleme.cn/#/zh-CN/component/radio 参考:https://www.cnblogs.com/steamed-twisted-rol ...

  6. vue开发中控制台报错问题

    1.sockjs.js?9be2:1606 GET http://localhost:8566/sockjs-node/info?t=1569478261510 net::ERR_CONNECTION ...

  7. SELinux安全方式

    一.SElinux配置文件 在CentOS 7系统中部署SELinux非常简单,由于SELinux已经作为模块集成到内核中,默认SELinux已经处于激活状态.对管理员来说,更多的是需要配置与管理SE ...

  8. ubuntu 下 安装xdebug

    root@homestead:/etc/php/7.1/fpm/conf.d# vim 20-xdebug.ini zend_extension=xdebug.so xdebug.remote_ena ...

  9. MAC中怎么安装python

    转自:https://blog.csdn.net/hou_manager/article/details/79555809 一.Python 介绍 Python介绍 Python3在2008年12月3 ...

  10. Linux 下用Win共享

    让win能访问到 linux 的smb 共享: linux 安装 samba: apt-get install samba #然后 vim /etc/samba/smb.conf #找到securit ...