Leetcode696.Count Binary Substrings计算二进制字串
给定一个字符串 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计算二进制字串的更多相关文章
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- HDU 4588 Count The Carries 计算二进制进位总数
点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- Python3解leetcode Count Binary Substrings
问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
随机推荐
- 2019-8-31-dotnet-通过-WMI-获取系统安装的驱动
title author date CreateTime categories dotnet 通过 WMI 获取系统安装的驱动 lindexi 2019-08-31 16:55:59 +0800 20 ...
- 2018-12-18-WPF-一个空的-WPF-程序有多少个窗口
title author date CreateTime categories WPF 一个空的 WPF 程序有多少个窗口 lindexi 2018-12-18 21:16:40 +0800 2018 ...
- Install- Linux必学的60个命令
1.作用 install命令的作用是安装或升级软件或备份数据,它的使用权限是所有用户. 2.格式 (1)install [选项]... 来源 目的地 (2)install [选项]... 来源... ...
- Leetcode942. DI String Match增减字符串
给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length. 返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i ...
- 跟我一起学习webpack使用配置文件(二)
接着跟我一起学习webpack(一)中的项目来,我们接下来使用配置文件 使用npx webpack -h 我们可以查看webpack的配置参数 从我们在package.json中添加的命令来看,当项目 ...
- parameter -- tWR
http://www.samsung.com/global/business/semiconductor/file/product/tWR-0.pdf tWR: write recovery time ...
- Python开发利器Pycharm
可以新建项目的时候,在下面的小扳手那里创建虚拟化环境.若想安装包,直接file->setting里面找到对应项目,然后添加新的包即可. 视频地址:http://edu.51cto.com/c ...
- 我的js运动库新
1.一些样式的获取和设置 //通过id获取当前元素 //params:id function $id(id) { return document.getElementById(id); } //向cs ...
- string主要操作函数
参考博客:http://blog.csdn.net/zhenyusoso/article/details/7286456 std::string illegal(" \t\f\v\n\r\\ ...
- JMETER远程运行_多机联合负载
JMETER远程运行_多机联合负载 远程运行是用一台JMeter控制机控制远程的多台机器来产生负载.控制机与负载机之间通过RMI方式来完成通信.在负载机上运行Agent程序(启动命令是%JMETER_ ...