LeetCode 696 Count Binary Substrings 解题报告
题目要求
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.
题目分析及思路
给定一个字符串,要求得到所有满足条件的非空连续子串的数目。子串需有相等数量的0和1,并且0和1都是各自连续的。若出现相同的子串则计算它们出现的次数。可以先确定原字符串中连续出现0或1的长度,存为数组groups。之后遍历groups,只要该数组元素保持不变或增大,则能确保原字符串中对应区间里的字符都能找到满足条件的子串;若变小,则只能取groups中较小元素的值m,说明此时只有m个字符能找到满足条件的子串。
python代码
class Solution:
def countBinarySubstrings(self, s: str) -> int:
groups = [1]
for i in range(1,len(s)):
if s[i-1] == s[i]:
groups[-1] += 1
else:
groups.append(1)
ans = 0
for i in range(1,len(groups)):
if groups[i-1] <= groups[i]:
ans += groups[i-1]
else:
ans += groups[i]
return ans
LeetCode 696 Count Binary Substrings 解题报告的更多相关文章
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- [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 ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
- 【LeetCode】401. Binary Watch 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】868. Binary Gap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...
随机推荐
- 关于docker 意外停止,重新快速启动措施
1. 我们要重启这个镜像,需要知道这个镜像ID,类似这个: 7079ff99e10ac326726a364348853c0e508cad8ce00ae970f3c800f172a40252 那么你可以 ...
- c#异常重试机制
有时候我们碰到程序异常了,想让程序继续重新执行,进行重试,这时候就需要有一个合适的方法来进行操作: 自己写代码控制太麻烦了,也容易出错.这时候当然是站在巨人的肩膀上, https://github.c ...
- Linux内核剖析(三)构建源码树
linux源码树结构 参考 http://www.360doc.com/content/13/0410/17/7044580_277403053.shtml 目录 描述 arch 目录包括了所有和体系 ...
- eclipse工具中使用Data Source Explorer连接数据库(MySQL)
1.进入Eclipse工具,打开Data Source Explorer.Window==>Show View==>Data Source Explorer(注:如果找不到请选择Other ...
- PHP为JSON数据的API返回空数组或者空对象
在使用 JSON 作为 API 数据 Content-Type 的时候,会有这样一个问题: 如何返回一个空对象和一个空数组? 使用:json_encode(array()) 得到JSON结果:[] ...
- asp.net中 使用参数化mysqlparameter 保存数据时,总保存成一个汉字的解决方案。
var param = new MySqlParameter("@" + columName, property.Value); param.DbType = DbType.Str ...
- [ci]容器ci索引
伙计们: 有任何意见或建议或看不懂的请在对应的文章下留言(请注明上下文) 我会及时改动. 这是以前的一些在物理机上搞过 [ci]容器ci索引 http://www.cnblogs.com/iiiihe ...
- 20款最好的JavaScript开发框架
JavaScript语言有多种方式,创建交互式网站,Web应用程序.基本的JavaScript框架是预先写好的JavaScript代码集.这些JavaScript框架也被称为JavaScript库,开 ...
- 【资料下载区】【GMT43相关代码、资料下载地址】更新日期2017/06/28
[GMT43相关文档][更新中...] GMT43原理图(PDF)下载GMT43说明书(PDF)下载GMT43机械结构尺寸(PDF)下载 [GMT43相关例程代码][ARM][更新中...] 基于HA ...
- 【iCore4 双核心板_ARM】例程三十三:SD_IAP_ARM实验——更新升级STM32
实验现象及操作说明: 1.本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中. 2.直接上电或烧写程序将执行升级的APP应用程序. 3.按下按键上电或写程序将进行升 ...