LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/
题目:
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.
题解:
Count consecutive 0s or 1s.
e.g. 0001111, 就是3个0, 4个1.
When current pointing char is different. Add the min(preCount, curCount) to res.
Time Complexity: O(s.length()). Space: O(1).
AC Java:
class Solution {
public int countBinarySubstrings(String s) {
if(s == null || s.length() == 0){
return 0;
}
int n = s.length();
int preCount = 0;
int curCount = 0;
int res = 0;
int i = 0;
while(i<n){
char cur = s.charAt(i);
while(i<n && s.charAt(i) == cur){
curCount++;
i++;
}
res += Math.min(preCount, curCount);
preCount = curCount;
curCount = 0;
}
return res;
}
}
LeetCode Count Binary Substrings的更多相关文章
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- Python3解leetcode Count Binary Substrings
问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- 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
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- [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 ...
随机推荐
- 每天一个Linux命令(58)sudo命令
sudo命令用来以其他身份来执行命令,预设的身份为root. (1)用法: 用法: sudo [参数] [命令] (2)功能: 功能: sudo可以针对 ...
- php获取MAC地址
/** *获取mac地址 **/ class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 var $mac_addr; fun ...
- QT 数字图像处理 笔记一
1.被有符号整数和无符号整数十足的坑了一上午.我在实现图像旋转的时候先把坐标轴中心平移到图像中心:painter.translate(up_x+temp_w,up_y+temp_h);注意这里面各个数 ...
- post请求和get请求content_type的种类
get请求的headers中没有content-type这个字段,post 的 content-type 有两种 : application/x-www-form-urlencoded 这种就是一般的 ...
- blast+学习之search tools
search tools:blastn, blastp, blastx, tblastx, tblastn, psiblast, rpsblast, and rpstblastn 1.blastn: ...
- 如何用hexo+github搭建个人博客
搭建环境 1.安装 Node.js: https://nodejs.org/en/ windows下点击链接,下载安装即可;Linux下更加简单,在终端下输入sudo apt-get install ...
- Kubernetes 待学习列表
1.EFK or ELK https://blog.csdn.net/mawming/article/details/78344939, https://www.jianshu.com/p/fe3ac ...
- CentOS 7 导入epel库
yum install epel-release 或者到百度云下载相应的 rpm 包进行安装 rpm -ivh epel-release-7-9.noarch.rpm
- MongoDB快速入门(八)- 删除文档
删除文档 MongoDB 的 remove()方法用于从集合中删除文档.remove()方法接受两个参数.一个是标准缺失,第二是justOne标志 deletion criteria : 根据文件(可 ...
- Anaconda创建环境、删除环境、激活环境、退出环境
Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6 删除环境(不要乱删啊啊啊) conda re ...