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 ...
随机推荐
- iOS--App功耗优化
良好的用户体验需要如下要素: 电池寿命长.随着能效降低,电池寿命也会降低.但用户想让自己的移动设备全天候待命. 速度快.iOS系统处理复杂操作时仍能提供很好的性能. 响应快.同一时刻消耗太多资源会使U ...
- 解决matplotlib库在PyCharm和命令行都无法正常显示问题
我们在学习人工智能的时候,会经常用到matplotlib,在学习的时候有一些例子写了代码运行: import matplotlib.pyplot as plt import numpy as np x ...
- Data Model for Certificate
- python3 + flask + sqlalchemy +orm(1):链接mysql 数据库
1.pycharm中新建一个flask项目 2.按装flask.PyMySQL.flask-sqlalchemy 3.项目下面新建一个config.py 文件 DEBUG = True #dialec ...
- 框架Thinkphp5 简单的实现行为 钩子 Hook
这篇文章主要介绍了关于框架Thinkphp5 简单的实现行为 钩子 Hook,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 实现在一个方法开始和结束加入两个行为:api_init.ap ...
- 物联网架构成长之路(28)-Docker练习之MQ中间件(Kafka)
0. 前言 消息队列MQ,这个在一般的系统上都是会用到的一个中间件,我选择Kafka作为练手的一个中间件,Kafka依赖Zookeeper.Zookeeper安装上一篇博客已经介绍过了. 1. Kaf ...
- ero-configuration Web Application Debugging with Xdebug and PhpStorm
1. Install Xdebug To use Xdebug with PhpStorm for debugging PHP applications, you need to have a PHP ...
- LaTeX数学模式&上下标&代码块
效果就是如上图所示了.学习了使用数学模式插入公式和使用上标和公式的编号.这里的目录没有展开,在编译一次目录会展开,代码块会被挤到下一页上面去. \documentclass[UTF8]{ctexart ...
- 快速开发项目,用到的工具:UI 设置利器 sketch
需求设计: axaure8.0 tool: teambition/石墨.幕布. 接口管理tool(后端开发接口,pc,m,app使用) https://www.eolinker.com/#/ ui 设 ...
- <数字签名是什么>笔记
此篇是看 数字签名是什么? 的理解与笔记 1. 使用非对称密钥发送数据到服务器. 发送方(C端)使用公钥对自己的数据进行加密,接收方(S端)使用私钥解密,即使发送出去的数据被拦截到,也不知道 ...