[LeetCode] 696. Count Binary Substrings_Easy
利用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的更多相关文章
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- [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 ...
- 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统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- leetcode 696
696. Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 substrin ...
随机推荐
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- [No000016F]高并发下线程安全的单例模式(最全最经典)
在所有的设计模式中,单例模式是我们在项目开发中最为常见的设计模式之一,而单例模式有很多种实现方式,你是否都了解呢?高并发下如何保证单例模式的线程安全性呢?如何保证序列化后的单例对象在反序列化后任然是单 ...
- [No0000CA]什么是“普瑞马法则”?以及 如何利用“普瑞马法则”,三步克服惰性
一般在学习和生活中,我们都可能有这样的经验,就是当说想要作某件事情的时候,但过了好久发现还是没有做:或者觉得有力气使不出来:或者总觉得生活是灰色和抑郁的等等. 这类情况反映在生活中,就是生活好像总是被 ...
- Zabbix使用微信发送告警(附Python代码)
介绍 本文将介绍如何把zabbix告警接入到微信,通过微信企业号将告警信息发送到运维人员的微信上.本文适合于已经实现了邮件告警的小伙伴,因为需要在已经能实现告警的基础上进行,如果还不知道如何配置zab ...
- jQuery 报错,对象不支持tolowercase属性或方法
泪流满面.<input>里id和name都不能是nodeName,否则跟jquery.js冲突 JQuery 实践问题 - toLowerCase 错误 在应用JQuery+easyui开 ...
- PHP之数组函数
php数组中的预定义变量 预定义常量 CASE_LOWER (integer) CASE_LOWER 用在array_change_key_case()中将数组的键名转换为小写字母.这也是array_ ...
- 转:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
原文地址:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析 前言 本文将分析mybatis与spring整合的MapperScannerConfigur ...
- Vue中父子组件执行的先后顺序
Vera Vue中父子组件执行的先后顺序探讨(转载) 前几天,朋友向我提出了一个关于Vue中父子组件执行的先后顺序问题,相信很多朋友在学习的过程中也会遇到这个问题,所以我就在此提出我自己的一些小看 ...
- 【Python爬虫】PyQuery解析库
PyQuery解析库 阅读目录 初始化 基本CSS选择器 查找元素 遍历 获取信息 DOM操作 伪类选择器 PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎 ...
- 连连看java版
主界面 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt. ...