作者: 负雪明烛
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. Selenium-IDE,在网页上模拟人的操作

    想偷懒,不想做很机械重复的网页操作,就百度了一下看看有什么方法,能把自己从重复性的网页操作中解放出来,于是,百度到了selenium ide,折腾许久,用最新版火狐添加了自带selenium ide组 ...

  2. react native 导航器 Navigator 简单的例子

    最近学习react native 是在为全栈工程师而努力,看网上把react native说的各种好,忍不住学习了一把.总体感觉还可以,特别是可以开发android和ios这点非常厉害,刚开始入门需要 ...

  3. URLDNS分析

    学习了很久的Java基础,也看了很多的Java反序列化分析,现在也来分析学习哈最基础的URLDNS反序列化吧. Java反序列化基础 为了方便数据的存储,于是乎有了现在的Java序列化于反序列化.序列 ...

  4. express系列(1)概述

    在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...

  5. Linux学习 - 挂载命令

    一.mount 1 功能 将外设手工挂载到目标挂载点 2 语法 mount  [-t 文件系统]  [设备文件名]  [挂载点] 3 范例 mkdir  /mnt/cdrom 在/mnt下创建一个cd ...

  6. Spring Batch : 在不同steps间传递数据

    参考文档: How can we share data between the different steps of a Job in Spring Batch? Job Scoped Beans i ...

  7. clickhouse输入输出格式 TSKV CSV

    TSKVTSKV格式不适合有大量小列的输出.TSKV的效率并不比JSONEachRow差.TSKV数据查询和数据导入.不需要保证列的顺序. 支持忽略某些值,这些列使用默认值,例如0和空白行.复杂类型的 ...

  8. ES在项目中的测试

    1.application.yml server: port: ${port:40100}spring: application: name: xc-search-servicexuecheng: e ...

  9. Linux脚本教程

    Linux_Shell_脚本参数接收键盘输入 #!/bin/bash #提示"请输入姓名"并等待30秒,把用户的输入保存入变量name中 read -t 30 -p "请 ...

  10. Redis单点到集群迁移

    目录 一.简介 一.简介 1.环境 源 192.168.1.185的6379 目标 192.168.1.91的7001,7002 192.168.1.92的7003,7004 192.168.1.94 ...