leetcode 696
696. Count Binary Substrings
Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat and are counted the number of times they occur.
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
First, I count the number of or grouped consecutively.
For example "" will be [, , , ]. Second, for any possible substrings with and grouped consecutively, the number of valid substring will be the minimum number of and .
For example "", will be min(, ) = , ("", "", "") ////////////////////// int countBinarySubstrings(string s) {
int cur = , pre = , res = ;
for (int i = ; i < s.size(); i++) {
if (s[i] == s[i - ]) cur++;
else {
res += min(cur, pre); //主要是这里的技巧,比如"0001111", will bemin(3, 4) = 3
, ("01", "0011", "000111"
)
pre = cur;
cur = ;
}
}
return res + min(cur, pre);
}
leetcode 696的更多相关文章
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- Java实现 LeetCode 696 计数二进制子串(暴力)
696. 计数二进制子串 给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的. 重复出现的子串要计算它们出现的次数. 示例 1 ...
- 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_Easy
利用group, 将每个连着的0或者1计数并且append进入group里面, 然后再将group里面的两两比较, 得到min, 并且加入到ans即可. T: O(n) S: O(n) 比较 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- leetcode(js)算法之696计数二进制串
给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的. 重复出现的子串要计算它们出现的次数. 示例: 输入: "0011 ...
随机推荐
- oracle将查询结果横转纵
SELECT '残疾人|民政|综合治理|计划生育|物业监管|安全生产|环境类|司法信访|党建|社会组织|文化体育|社保' D , '53|52|51|50|49|48|47|5|4|3|2|1' g ...
- 大杀器Bitset
其实并不怎么会用,有一次有位学长提到了这个名字,就这么取题目了. 1.BZOJ 3687 简单题 求子集的算术和的异或和 http://www.lydsy.com/JudgeOnline/proble ...
- 多线程的基本概念和Delphi线程对象Tthread介绍
多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...
- Laravel5.4中自定义404等错误页面
1.在resources/views/下简历文件夹error,在error文件中建立"404.blade.php文件". <!DOCTYPE html PUBLIC &quo ...
- docker 可持续集成及日志管理及监控报警
- icon 的前生今世 & iconfont 的晋级之路
布吉岛为啥起了个这么文(dou)艺(bi)的名字,话不多说,开始总结
- ElasticJob-分布式作业调度神器,你们还在用Quartz吗?!
简介 Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成. Elastic-Job-Lite定位为轻量级无中 ...
- 使用maven搭建Hibernate
使用maven搭建Hibernate框架(web项目) create table USERS ( ID NUMBER not null primary key, NAME ), PASSWORD ), ...
- django汉化
汉化admin后台管理站点 修改settings文件(将上表格内容改变为下表格): LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' LANGUAGE_CODE = ...
- python语句结构(while循环)
while循环 pythhon中while语句的一般形式 while 判断语句: 执行语句 i=0 sum=0 while i<=100: sum+=i i=i+1 print(sum) #运行 ...