[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 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.
Example 1:
Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat and are counted the number of times they occur.
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
Example 2:
Input: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
Note:
s.lengthwill be between 1 and 50,000.swill only consist of "0" or "1" characters.
class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
ans=0 for i in range(len(s)-1):
if s[i]!=s[i+1]:
ans+=self.findBS(i,s) return ans def findBS(self,i,s):
count=1
while i-count>=0 and i+1+count<len(s) and s[i-count]==s[i] and s[i+1]==s[i+1+count]:
count+=1
return count
[LeetCode&Python] Problem 696. Count Binary Substrings的更多相关文章
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- 【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 numbe ...
- 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
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
随机推荐
- kali-rolling安装nessus 7并创建扫描任务教程
一.下载 下载页面:https://www.tenable.com/downloads/nessus 如果自己安装的kali是32位的则选择上边的32位版本下载 二.安装 直接用dpkg安装即可: d ...
- rdesktop安装教程
1.介绍 rdesktop是Linux下连接windows远程桌面的工具 2.下载 https://github.com/rdesktop/rdesktop/releases 3.安装 mkdir / ...
- Python Number 类型转换
int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real [,imag ]) 创建 ...
- 微信UnionId 部分开放
以前要获得UnionID, 需要把公众号绑定到微信开放平台, 这个微信开放平台垃圾,还要300认证费. 今天突然发现在这个接口 https://api.weixin.qq.com/cgi-bin/us ...
- Mysql索引引起的死锁
提到索引,首先想到的是效率提高,查询速度提升,不知不觉都会有一种心理趋向,管它三七二十一,先上个索引提高一下效率..但是索引其实也是暗藏杀机的... 今天压测带优化项目,开着Jmeter高并发访问项目 ...
- QuickStart系列:docker部署之Mysql
这里配置只做开发用,生产环境请根据需要修改或自行搜索其他说明 使用docker安装mysql,目前版本5.7.4(当前时间 2018.1.11) 环境 vm: Centos7 镜像来源 https:/ ...
- 微信授权(Net Mvc)
项目结构 WeiXinController.cs using System; using System.Collections.Generic; using System.Linq; using Sy ...
- bzoj1968
题解: 显然每一个数对答案的贡献为n/i 代码: #include<bits/stdc++.h> using namespace std; int n; int main() { scan ...
- day36 数据库表操作 数据类型 完整性约束
今日内容 1.存储引擎表类型 2.数据类型 3.完整性约束 1.存储引擎表类型 指令: 1.show engines:#查看MySQL所有的引擎, 2.show variables like &quo ...
- elasticsearch基本操作之--使用QueryBuilders进行查询
/** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ QueryBuilder 是es中提供的一个查询接口, 可以 ...