[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 ...
随机推荐
- Jenkins插件管理
1.配置jenkins需要的maven.jdk路径 [root@db01 secrets]# echo $JAVA_HOME /application/jdk [root@db01 secrets]# ...
- SecureCRT安装使用
● 解决自动断开 echo "TMOUT=6000 " >>/etc/profile source /etc/profile 在连接上右键属性,然后“终端”,“反空闲” ...
- [No0000120]Python教程3/9-第一个Python程序
现在,了解了如何启动和退出Python的交互式环境,我们就可以正式开始编写Python代码了. 在写代码之前,请千万不要用“复制”-“粘贴”把代码从页面粘贴到你自己的电脑上.写程序也讲究一个感觉,你需 ...
- C语言中点操作符(.)和箭头操作符(->)
C语言中点操作符(.)和箭头操作符(->) 点说语法不太准确,许多都称该之为点运算符/操作符,箭头运算符/操作符.但是OC中叫点语法,感觉理解起来还蛮舒服.毕竟基础的C操作符中是 相同点 两个都 ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- scala-LinkedList
LinkedList每隔元素乘以3: import scala.collection.mutable.LinkedList var list1=LinkedList.apply(1,2,3,4,5) ...
- 不看好运维竖井产品模式,优云打造融合化运维PaaS平台
2018年1月13号中国双态运维用户大会上,优云软件总裁刘东海接受了36Kr记者的专访,期间谈到了新时代下的企业运维模式,新兴技术和传统运维的融合以及优云未来的发展方向等问题.以下为访谈实录: 优云软 ...
- nodejs 学习三 异步和同步
同步函数 for (let i = 0; i < 10; i ++) { setTimeout(() => { console.log(`${i} ______ ${new Date}`) ...
- es中filter和query的对比
1.filter与query示例PUT /company/employee/2{ "address": { "country": "china&quo ...
- 如何使用CryptoJS配合Java进行AES加密和解密
注意 1. PKCS5Padding和PKCS7Padding是一样的 2. 加密时使用的key和iv要转换成base64格式 一.前端 1.函数 function encrypt (msg, key ...