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

improve: 思路相同, 用pre和cur去将space节省到O(1)

Code

1)  T: O(n)   S: O(n)

class Solution:
def countBinarySubstrings(self, s):
group = [1]
for i in range(1,len(s)):
if s[i] != s[i-1]:
group.append(1)
else:
group[-1] += 1
ans = 0
for i in range(len(group) -1):
ans += min(group[i], group[i+1])
return ans

2) T: O(n)   S: O(1)

class Solution:
def countBinarySubstrings(self, s):
ans, pre, cur = 0, 0, 1
for i in range(1,len(s)):
if s[i] != s[i-1]:
ans += min(pre, cur)
pre, cur = cur, 1
else:
cur += 1
return ans + min(pre, cur)

[LeetCode] 696. Count Binary Substrings_Easy的更多相关文章

  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. LeetCode 696 Count Binary Substrings 解题报告

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

  3. 696. Count Binary Substrings - LeetCode

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

  4. 【Leetcode_easy】696. Count Binary Substrings

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

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

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

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

  7. 696. Count Binary Substrings

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

  8. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  9. leetcode 696

    696. Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 substrin ...

随机推荐

  1. 安装 powerline

    使用说明: https://powerline.readthedocs.io/en/latest/usage.html ~ vim,在 .vimrc 中添加配置 set rtp+=/usr/lib/p ...

  2. dts中memreserve和reserved-memory的区别 (转)

    https://blog.csdn.net/kickxxx/article/details/54631535

  3. ES6 import

    ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...

  4. POJ 2027 - No Brainer

    Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indica ...

  5. python与pip安装

    # Install pip for 2.7 and then python 2.7 itself sudo apt install python-pip sudo apt install python ...

  6. Sethi model

    小结: 1. 销量 广告 微分方程 动态系统 市场份额 https://en.wikipedia.org/wiki/Sethi_model The Sethi model was developed ...

  7. opencv -python

    https://www.python----------tab.com/html/2017/pythonhexinbiancheng_1120/1184.html http://www.cnblogs ...

  8. 【Mysql数据库访问利器】phpMyadmin

    缘由 我们程序员难免要和数据库打交道,经过这几年的锻炼,感觉手写SQL语句已经忘记的差不错了,促使我一定要这篇文章的原因是,有一次晚上我更新某个系统的数据库的表(由于目前公司比较严格,数据库都只能通过 ...

  9. 转:cookie.setPath()用法

    原文地址:cookie.setPath()的用法 正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得. 1.可在同一应用服务器内共享方法:设置cookie.setPath ...

  10. python摸爬滚打之day09----初识函数

    1.函数 把一段公共的代码提取出来通过一个变量(函数名)将这些代码重复调用, 使程序可拓展易维护. def 函数名(形参): 函数体 函数名(实参)  -----> 调用该函数 2.return ...