[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 ...
随机推荐
- python操作文件
OS模块 1.getcwd() 用来获取当前工作目录 >>> import os >>> os.getcwd() 'D:\\Postgraduate\\Python ...
- 在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误
32位机器删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\version\DataProviders\{7C602B5B-ACCB-4acd ...
- xiv存储操作
XIV存储操作维护手册 二○一二年七月 目录 1. 存储划分... 3 1.1. 定义Storage Pool 3 1.2. ...
- nowcoder 211B - 列队 - [(伪·良心贪心)真·毒瘤暴力]
题目链接:https://www.nowcoder.com/acm/contest/211/B 题目描述 炎热的早上,gal男神们被迫再操场上列队,gal男神们本来想排列成x∗x的正方形,可是因为操场 ...
- [No0000134]C#中的委托,匿名方法和Lambda表达式
简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...
- [No0000121]Python教程4/9-输入和输出
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...
- 第一次java程序测试感受
第一次JAVA程序设计测试,检验了一个暑假的成果.显而易见,我做的并不是很好,程序最起码的输入输出以及方法的定义还是没有问题的,但是考到了文件输入输出便看出来了.对于文件的输入输出,虽然我预习到那里, ...
- Elastic数据迁移方法及注意事项
需求 ES集群Cluster_A里的数据(某个索引或某几个索引),需要迁移到另外一个ES集群Cluster_B中. 环境 Linux:Centos7 / Centos6.5/ Centos6.4Ela ...
- 超级有用的15个mysqlbinlog命令
在MySQL或MariaDB中,任意时间对数据库所做的修改,都会被记录到日志文件中.例如,当你添加了一个新的表,或者更新了一条数据,这些事件都会被存储到二进制日志文件中.二进制日志文件在MySQL主从 ...
- MVC 实用架构设计(三)——EF-Code First(5):二级缓存
一.前言 今天我们来谈谈EF的缓存问题. 缓存对于一个系统来说至关重要,但是是EF到版本6了仍然没有见到有支持查询结果缓存机制的迹象.EF4开始会把查询语句编译成存储过程缓存在Sql Server中, ...