利用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. H - Being a Good Boy in Spring Festival

    一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 如果愿意 你还 ...

  2. day5 五、数字类型、字符串,列表类型的基本操作和内置方法

    一.可变与不可变 可变:值改变,但是id不变,证明就是在改变原值,是可变类型.它的原理是在内存里有一个值,然后这个值发生了改变,意为id地址是同一个,没有变化 # l=['a','b'] # prin ...

  3. vmware 中安装Ghost XP 版本心得

    安装是肯定是选择 ISO映像文件,第一次进入真能进入Ghost选择界面, 无论你第一次 进入的是pe 或 一键分区还是 ghost到C盘最后你再重启就总是让你按任意键或Ctrl+Alt+Del 自然想 ...

  4. tensorflow的variable的eval()和read_eval()有什么不同

    eval()返回的数值标量 read_eval()返回的是这个变量的tensor,类型是read 直接上代码: def tensoflow_test(): t = tf.Variable(initia ...

  5. 解决GP服务产生的结果无法自动发布为地图服务的问题

    在ArcGIS for Javascript API或REST API调用GP服务时,常常会遇到这样一种情况:GP服务运行以后,执行成功,也能够生成结果,然而结果并没有直接产生动态的地图服务供API调 ...

  6. a buzzword to refer to modern Web technologies

    https://html.spec.whatwg.org/multipage/introduction.html#is-this-html5? HTML Living Standard — Last ...

  7. [about remote controller]--mstsc-teamviewer-vnc,nomachine

    https://www.jianshu.com/p/c80db368ed8a https://www.nomachine.com/download Ubuntu安装VNC,VNC却无法随系统启动,遂换 ...

  8. 《HTTP - http报文》

    还时推荐一首歌 - 那吾克热<纸飞机> 有没有突然想要个孩子的冲动,哈哈. 读 第三章<HTTP报文内的HTTP信息> 总结 1:用于HTTP协议交互叫做HTTP报文,请求端( ...

  9. tomcat端口设置

    在tomcat安装目录下,编辑/conf/server.properties 更改对应的端口: 然后系统重启就可以了.

  10. 用Pyinstaller 实现py.转化为exe可执行文件----二维码实例

    1,安装 Pyinstaller 命令提示符窗口:pip install pyinstaller 2,制作二维码脚本 d5_code.py from MyQR import myqr #生成二维码 w ...