作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/baseball-game/description/

题目描述

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.

Note:

  1. s.length will be between 1 and 50,000.
  2. s will only consist of “0” or “1” characters.

题目大意

一个字符串由01组成,现在需要寻找满足连续子字符串中01个数相等的子字符串的个数。如果在不同位置出现的子字符串,不要去重计数。

解题方法

方法一:暴力解法(TLE)

看了s的长度那么大,估计暴力解法会超时,果然就超时了。但是做法很简单,只需要从每个位置开始向后数,数到0的个数和1的个数相等时候停止就好了。每次不需要遍历到结尾,所以最坏时间复杂度是O(N^2),最优时间复杂度是O(N)。但是没有通过。

class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
res = 0
for i in range(N):
c1, c0 = 0, 0
if s[i] == "1":
c1 = 1
else:
c0 = 1
for j in range(i + 1, N):
if s[j] == "1":
c1 += 1
else:
c0 += 1
if c0 == c1:
res += 1
break
return res

方法二:连续子串计算

首先,数一下,连续的0,1的个数有多少,构成一个数组。比如,“0110001111”的连续0和1的个数是[1, 2, 3, 4].

然后,我们想求得0和1的个数相等的子串,所以需要进行一个交错,找出相邻的两个个数的最小值就好了。比如“0001111”, 结果是min(3, 4) = 3, 即,("01", "0011", "000111")

有什么道理呢?因为我们求得字符串出现的数组,它的每个位置一定是0,1交错的子字符串长度。否则相邻的0或者1会拼成更长的长度。所以我们最后求交错的最小值,就是得到了相邻字符串的0和1相等的长度。

根据上面的思路,可以写出这个代码:

class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
curlen = 1
res = []
for i in range(1, N):
if s[i] == s[i - 1]:
curlen += 1
else:
res.append(curlen)
curlen = 1
res.append(curlen)
return sum(min(x, y) for x, y in zip(res[:-1], res[1:]))

上面的代码可以写的更简洁:

class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
s = map(len, s.replace('01','0 1').replace('10','1 0').split())
return sum(min(i, j) for i,j in zip(s, s[1:]))

日期

2018 年 1 月 27 日
2018 年 11 月 10 日 —— 欢度光棍节

【LeetCode】696. Count Binary Substrings 解题报告(Python)的更多相关文章

  1. LeetCode 696 Count Binary Substrings 解题报告

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

  2. LeetCode 696. Count Binary Substrings

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

  3. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  4. 696. Count Binary Substrings - LeetCode

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

  5. 【Leetcode_easy】696. Count Binary Substrings

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

  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. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  8. 【LeetCode】401. Binary Watch 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...

  9. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. R包 tidyverse 分列

    代码: 1 library(tidyverse) 2 separate(data = df,col=chr_pos,into=c("chr","pos"),se ...

  2. IO流的字节输入输出流(InputStream,OutputStream)

    字节输出流与文件字节输出流 文件存储原理和记事本打开文件原理 OutputStream及FileOutputStream import java.io.FileOutputStream; import ...

  3. 阿里云NAS性能测试

    测试方法:根据阿里云NAS官方文档进行测试 测试对象:性能型NAS,总容量1PB,已使用27.49GB(计算吞吐量时按30GB计算) 随机读IOPS测试 测试命令 fio -numjobs=1 -io ...

  4. euerka总结

    一.euerka的基本知识 1. 服务治理 Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理 在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系 ...

  5. Shell 打印文件的最后5行

    目录 Shell 打印文件的最后5行 题解-awk 题解-tail Shell 打印文件的最后5行 经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowc ...

  6. Python计算期权隐含波动率

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. Black-Scholes 将期权价格描述为标的价格.行权价.无风险利率.到期时间和波动性的函数.  V ...

  7. vim一键整理代码命令

    vim下写代码超实用代码格式整理命令,仅需四步 ①先使用 gg 命令使光标回到第一行 ②shift+v 进入可视模式 ③shift+g 全选 ④按下  =  即可 混乱的代码格式 四步整理以后 工整又 ...

  8. spring boot集成mybatis框架

    概述 中文官网:http://www.mybatis.cn 参考教程:https://www.w3cschool.cn/mybatis MyBatis Plus:http://mp.baomidou. ...

  9. Docker从入门到精通(一)——初识

    1.Docker 是什么? Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容 ...

  10. Jenkins获取jar包的快照号

    目录 一.简介 二.脚本 一.简介 主要用于打jar包的工程,显示快照包的名字.当jar打包完成后,会在target目录中,截取快照名. 二.脚本 1.脚本return-version.sh #!/b ...