给定一个字符串 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. NVIDIA驱动安装、CUDA安装、cudnn安装

    1.禁用 nouveau 驱动 sudo vim /etc/modprobe.d/nvidia-installer-disable-nouveau.conf 或者 sudo vim /etc/modp ...

  2. Django项目:CRM(客户关系管理系统)--77--67PerfectCRM实现CRM课程出勤排名

    # classtop_urls.py # ————————64PerfectCRM实现CRM课程排名详情———————— from django.conf.urls import url from b ...

  3. MUI使用vue示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. Trie性能分析之敏感词过滤golang

    package util import ( "strings" ) type Node struct { //rune表示一个utf8字符 char rune Data inter ...

  5. cookie中转注入

    用这个源码搭建网站找注入点http://192.168.226.129/shownews.asp?id=235 判断注入点,在后面加上'http://192.168.226.129/shownews. ...

  6. mybatis # $的区别

    1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 select i ...

  7. .NET2.0引用.NET3.5的System.Core.dll&Dapper在.NET2.0下的配置

    微软MSDN对.NET2.0,3.0,3.5的描述: .NET Framework 版本 2.0.3.0 和 3.5 是使用同一 CLR 版本 (CLR 2.0) 生成的. 这些版本表示单个安装的连续 ...

  8. 【html、CSS、javascript-13】前端框架Bootstrap

    1.Bootstrap前端框架:包含css样式.js插件.图标等 http://www.bootcss.com/ 2.Font Awesome:非常全的图标大全 https://fontawesome ...

  9. TZ_13_负载均衡-Robbin

    1.但是实际环境中,我们往往会开启很多个user-service的集群.此时我们获取的服务列表中就会有多个,到底该访问哪一个呢? 一般这种情况下我们就需要编写负载均衡算法,在多个实例列表中进行选择. ...

  10. 使用uni-app(Vue.js)创建运行微信小程序项目步骤

    使用uni-app(Vue.js)开发微信小程序项目步骤 1. 新建一个uni-app项目   创建完成后的目录结构 2. 打开微信小程序开发工具端的端口调试功能 3. 运行创建的项目 效果