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 ...
随机推荐
- loadrunder之脚本篇——接口传参为本地文件
导言 前几天需要对公司一个专门很重要的接口进行压测,这个还不是重点,重点是传参为本地的图片!刚刚开始通过web_custom_request()函数来解决,可是脚本并不能通过!后面又百度不到答案,通过 ...
- 每天一个Linux命令(46)ifconfig命令
在windows系统中,ipconfig命令行工具被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config). ( ...
- linux下从源代码安装git的问题(install from source)
安装环境:centos7.2 安装依赖包: yum install -y gcc .el7..x86_64 openssl-devel.x86_64 yum install -y curl.x86_6 ...
- js怎么将光标移动特定的位置:
第一种方法: a 标签的锚: 将a标签的herf='#element_id_name' 即可 <a href="#comment_content" class=" ...
- php数组函数-array_diff()
array_diff()函数返回两个数组的差集数组.该数组包括了所有在被比较数组中,但是不在任何其他参数数组中的键值在返回数组中,键名保持不变. array_diff(array1,array2,ar ...
- 20145231第二周Java学习笔记
20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...
- 何为RunLoop?RunLoop有哪些应用场景?
一.RunLoop的作用 一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件发生 ...
- INSPIRED启示录 读书笔记 - 第40章 最佳实践经验
十大要点 1.产品管理的职责:许多产品经理将大把的时间浪费在与产品管理无关的工作上 2.用户体验:对于大多数软件产品来说,用户体验就是产品的生命 3.机会评估:用方便快捷的机会评估方法取代过时的市场需 ...
- MIPI协议中文详解【转】
本文转载自:http://www.voidcn.com/blog/michaelcao1980/article/p-6254588.html 一.MIPI MIPI(移动行业处理器接口)是Mobile ...
- HTML5 上传图片 到ASP.NET MVC
@{ ViewBag.Title = "Home Page"; } <!DOCTYPE HTML PUBLIC> <html> <head> & ...