[抄题]:

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给出的题目变变变]:

[代码风格] :

  1. 打草稿的时候,框架写在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个数的更多相关文章

  1. 【Leetcode_easy】696. Count Binary Substrings

    problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...

  2. 696. Count Binary Substrings - LeetCode

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

  3. [LeetCode] Count Binary Substrings 统计二进制子字符串

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

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

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

  5. LeetCode 696. Count Binary Substrings

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

  6. 696. Count Binary Substrings

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

  7. [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 ...

  8. LeetCode 696 Count Binary Substrings 解题报告

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

  9. LeetCode算法题-Count Binary Substrings(Java实现)

    这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...

随机推荐

  1. onsubmit对表单的拦截

    今天遇到一个问题:在对同name 的input 表单时 判断其值是否有效 用了each判断 当初错误的做法: function check_goods() { var regs = /^\d+$/; ...

  2. Java JDK安装和配置(Windows)

    安装和配置JDK JDK中自带了JRE,不需要单独下载, 打开JDK安装, 选择安装目录,下一步,装完JDK,会问是否安装JRE,选下一步, 最后还会问是否安装Java FX, 装完后就全部完成了JD ...

  3. H5打字机特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. matlab数据流仿真和时间流仿真

    simulink 使用的动态系统仿真,仿真需要求状态方程和输出方程,关键是求状态方程,而状态方程的求解有多种算法,可变步长和定步长,所以仿真时对求解器的选择和步长的设置就比较重要. 所谓基於数据流的仿 ...

  5. 解决ubantu中sublime不支持中文的方法

    更新然后将系统升级到最新版本,在linux终端输入 sudo apt-get update && sudo apt-get 在本地目录中克隆此repo:    如果你没有git的话就安 ...

  6. Oracle与Mysql操作表序列

    一.Oracle添加表序列 CREATE SEQUENCE name -- 序列名 INCREMENT BY -- 每次加几个 START WITH -- 从几开始计数 MINVALUE --- 最小 ...

  7. window下TortoiseGit的安装和使用

    一.安装git for windows 首先下载git for windows客户端https://git-for-windows.github.io/安装过程没什么特别的,下载完安装包之后,按照提示 ...

  8. Angular2使用boostrap和ng-bootstrap总结

    Angular2使用bootstrap有几种方式,本文主要介绍两种方式进行Boostrap样式的使用: 一.直接通过静态文件的方式进行引入: 通过命令新建一个Bootstrap的工程 ng new B ...

  9. 18.Selenium+Python案例 -- 豆瓣

    一.具体代码实现: from selenium import webdriver driver = webdriver.Firefox() driver.get('https://www.douban ...

  10. SpringMvc入门一----介绍

    Spring Mvc简介: Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求 ...