696. Count Binary Substrings统计配对的01个数
[抄题]:
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
Example 1:
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.
Example 2:
Input: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用stack,其实是文字游戏
[一句话思路]:
相等时统计长度,不相等时断绝关系
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
用prev curt即可判断配对
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
- 打草稿的时候,框架写在for的里面
class Solution {
public int countBinarySubstrings(String s) {
//cc
if (s.length() == 0) {
return 0;
}
//ini
int curLen = 1, preLen = 0, res = 0;
//for loop: for i to n - 1
//stop count or not
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
curLen++;
}else {
preLen = curLen;
curLen = 1;
}
//add res or not
if (preLen >= curLen) {
res++;
}
}
//return res
return res;
}
}
696. Count Binary Substrings统计配对的01个数的更多相关文章
- 【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 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode&Python] Problem 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
随机推荐
- 51nod 1995 三子棋
小的时候大家一定玩过“井”字棋吧.也就是在九宫格中,只要任意行.列,或者任意连续对角线上面出现三个相同的,就能获胜.现在小明和小花也在玩三子棋,但是他们不是在九宫格里,而是在3×4的格子里面.现在小明 ...
- block的基本使用
block用来保存一段代码 block的标志:^ block跟函数很像: 1. 可以保存代码 2. 有返回值 3. 有形参 4. 调用方式一样 定义bolock变量 例1: void (^myBloc ...
- php无wsdl webservice服务用法
服务端: <?php class test { function add($a,$b) { return $a+$b; } } function getUserInfo($name) { ret ...
- bzoj 2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...
- 关于yii2的中文乱码问题
在数据库配置中这么配就可以了 <?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0. ...
- ThinkPHP5.1开启调试和错误提示
app/config,php中找到 show_error_msg=false 改为True; 再将 'app_debug' => false 改为True;
- (转)C# Socket简单例子(服务器与客户端通信)
本文转载自:http://blog.csdn.net/andrew_wx/article/details/6629721 这个例子只是简单实现了如何使用 Socket 类实现面向连接的通信. 注意:此 ...
- Activity小技巧
随时随地退出程序 新建一个结合类对所有活动进行管理. public class ActivityCollector{ public static List<Activity> activi ...
- 【转】 Pro Android学习笔记(九三):AsyncTask(2):小例子
目录(?)[-] 继承AsyncTask UI操作接口 使用AsyncTask 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn. ...
- selenium中Xpath轴定位方法
1.Xpath轴:轴可定义相对于当前节点的节点集. 使用语法:轴名称::节点名称 例://input[@data-value="SXRYNAME"]/parent::td/foll ...