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 be min(3, 4) = 3, ("01", "0011", "000111")
pre = cur;
cur = ;
}
}
return res + min(cur, pre);
}

leetcode 696的更多相关文章

  1. LeetCode 696. Count Binary Substrings

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

  2. Java实现 LeetCode 696 计数二进制子串(暴力)

    696. 计数二进制子串 给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的. 重复出现的子串要计算它们出现的次数. 示例 1 ...

  3. LeetCode 696 Count Binary Substrings 解题报告

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

  4. [LeetCode] 696. Count Binary Substrings_Easy

    利用group, 将每个连着的0或者1计数并且append进入group里面, 然后再将group里面的两两比较, 得到min, 并且加入到ans即可.   T: O(n)   S: O(n)  比较 ...

  5. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  6. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

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

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

  8. 696. Count Binary Substrings - LeetCode

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

  9. leetcode(js)算法之696计数二进制串

    给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的. 重复出现的子串要计算它们出现的次数. 示例: 输入: "0011 ...

随机推荐

  1. thinkphp 正则路由

    正则路由也就是采用正则表达式定义路由的一种方式,依靠强大的正则表达式,能够定义更灵活的路由规则. 路由表达式支持的正则定义必须以“/”开头,否则就视为规则表达式.也就是说如果采用: '#^blog\/ ...

  2. react 16更新

    1.render新的返回类型 render方法支持两种新的返回类型:数组(由React元素组成)和字符串 2.错误处理 16之前,组件在运行期间如果执行出错,就会阻塞整个应用的渲染,这时候只能刷新页面 ...

  3. [kuangbin带你飞]专题一 简单搜索 - A - 棋盘问题

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  4. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  5. Linux文本编辑命令

    sort 排序工具,比较排序(根据字典排序) -t 指定分隔符(默认是空格) -k 指定第几域排序(默认第一域) -n 以数字大小排序 -r 逆向排序 -v 去掉重复行 -o 输出到文件中 -c 测试 ...

  6. 处理提交html危险代码的异常方法

    当向asp.net mvc提交有html标签的值时,服务器会报检测到潜在危险的Request.Form值,处理方法如下: 1:在接收处理的Action方法上面加上 [VaildateInput(fal ...

  7. amazeUI tab禁止左右滑动(触控操作)

    参考:http://amazeui.clouddeep.cn/javascript/tabs/ 效果: html: <!DOCTYPE html> <html> <hea ...

  8. 2019-8-31-dotnet-获取程序所在路径的方法

    title author date CreateTime categories dotnet 获取程序所在路径的方法 lindexi 2019-08-31 16:55:58 +0800 2019-03 ...

  9. 2019-10-4-C#-极限压缩-dotnet-core-控制台发布文件

    title author date CreateTime categories C# 极限压缩 dotnet core 控制台发布文件 lindexi 2019-10-04 14:59:36 +080 ...

  10. input光标使用caret-color改变颜色

    本文转载自:https://www.zhangxinxu.com/wordpress/2018/01/css-caret-color-first-line/ CSS caret-color属性可以改变 ...