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' 题意:给定一个由0和1组成的非空字符串,计算出由相同数量的0和1、且0和1分别连续的子串的个数。
思路1:直接暴力解决,但时间复杂度高,提交后time limit exceeded。从前往后遍历,判断当前数字的连续相同数字的数量,再找出从第一个不同数字开始,连续不同数字的数量,数量相同则count加1,否则count不变。
代码如下:
public int countBinarySubstrings(String s) {
char[] chars = s.toCharArray();
int count = 0;
for(int i = 0; i < chars.length - 1; i++){
if(isSubstrings(chars, i)){
count++;
}
}
return count;
}
public boolean isSubstrings(char[] chars, int start){
int same = start + 1;
while(same < chars.length - 1 && chars[start] == chars[same]){
same++;
}
int diff = same;
while(diff < chars.length && chars[diff] != chars[start] && diff - same < same - start){
diff++;
}
return diff - same == same - start ? true : false;
}

思路2:(LeetCode提供的算法1)在字符串s中,统计相同数字连续出现了多少次。例:s = "00110",则group = {2,2,1}。这样,在统计有多少个满足条件的子串时,对group数组从前往后遍历,依次取min{group[i], group[i+1]},再求和即可。

原因:以group[i]=2, group[i+1]=3为例,则表示“00111”或“11000”。
以“00111”为例,group={2,3},当第一个1出现时,前面已经有2个0了,所以肯定能组成01;再遇到下一个1时,此时有2个0,2个1,所以肯定能组成0011;再遇到下一个1时,前面只有2个0,而此时有3个1,所以不可以再组成满足条件的子串了。

代码如下:

public int contBinarySubstrings(String s){
char[] chars = s.toCharArray();
int[] group = new int[chars.length];
int index = 0;
group[0] = 1;
for(int i = 1; i < chars.length; i++){
if(chars[i] == chars[i - 1])
group[index]++;
else
group[++index] = 1;
}
int i = 0, count = 0;
while(i < group.length - 1 && group[i] != 0){
count += Math.min(group[i], group[i + 1]);
i++;
}
return count;
}

思路3:(LeetCode提供的算法2)定义两个变量pre和cur,pre存储当前数字前的数字连续次数,cur存储当前数字的连续次数。然后从第二个数字开始遍历,如果当前数字与前面数字相同,则cur自增1;否则对pre和cur取最小值,记为子串次数,并将pre赋值为cur,cur重置为1。原理与上一个方法相同,但没有定义数组,空间复杂度降低。

代码如下:


public int contBinarySubstring(String s){
int pre = 0, cur = 1, count = 0;
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) != s.charAt(i - 1)){
count += Math.min(pre, cur);
pre = cur;
cur = 1;
}
else{
cur++;
}
}
return count + Math.min(pre, cur);
}

 

LeetCode 696. Count Binary Substrings的更多相关文章

  1. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  2. 696. Count Binary Substrings - LeetCode

    Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...

  3. 【Leetcode_easy】696. Count Binary Substrings

    problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...

  4. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

  5. [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 ...

  6. 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  7. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  8. [LeetCode] 696. Count Binary Substrings_Easy

    利用group, 将每个连着的0或者1计数并且append进入group里面, 然后再将group里面的两两比较, 得到min, 并且加入到ans即可.   T: O(n)   S: O(n)  比较 ...

  9. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

随机推荐

  1. combotree -下拉框树异步加载

    问题: 下拉树数据比较多时,全加载会产生页面延迟,需要实现异步加载 方案: 点击事件加载:先加载部分,点击节点时再展开并追加子节点 onBeforeExpand事件:在展开树前加载,感觉这种方式比较优 ...

  2. 【MYSQL备份】利用mysqldump将一个数据库复制到另一个数据库

    假设要将服务器A上的数据库test备份到服务器B 1.在服务器B上新建数据库cp_test mysql> create database cp_test; Query OK, row affec ...

  3. Keil5的设置

    目录 编码格式 字体大小 代码颜色 编码格式 有时候用keil打开工程的时候,发现中文注释是乱码的格式,这是因为编码格式方式不对造成的.可以通过设置不同的编码方式来解决. 点击Edit->Con ...

  4. redis主从配置+sentinel哨兵

    redis主从配置+sentinel哨兵 1:编译环境准备 1.1环境确认 Redis是一个开源.支持网络.基于内存.键值对存储数据库,使用ANSI C编写.所以在搭建Redis服务器时需要C语言的编 ...

  5. 如何在HPUX的终端提示符前显示当前登录用户信息和所在目录

    修改/etc/default/profile文件,在最后加上如下内容: case $LOGNAME in     'root')     PS1="$LOGNAME@$(hostname): ...

  6. ovs源码阅读--流表查询原理

    背景 在ovs交换机中,报文的处理流程可以划分为一下三个步骤:协议解析,表项查找和动作执行,其中最耗时的步骤在于表项查找,往往一个流表中有数目巨大的表项,如何根据数据报文的信息快速的查找到对应的流表项 ...

  7. Elasticsearch 统计代码例子

    aggs avg 平均数 最近15分钟的平均访问时间,upstream_time_ms是每次访问时间,单位毫秒 { "query": { "filtered": ...

  8. python基础知识-8-三元和一行代码(推导式)

    python其他知识目录 1.三元运算(三目运算) 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值格式:[on_true] if [expression] else [on_false]re ...

  9. linux 性能分析命令及其解释

    很多时候,我们需要对linux上运行的环境大体有一个了解,那么久需要大体知道当前系统的相关资源的使用情况,那么可以用一些linux提供的丰富的命令来查看 性能分析 vmstat 虚拟内存统计 用法 U ...

  10. 跳蚤APP

    1.项目描述 我们致力于制作一款服务于校内师生的物品转让平台,在此平台上的用户可以发布要转让或欲购买的物品信息. 2.创新与收益 Need(需求) 如今在市场上有许多的二手交易线上平台,它们的服务范围 ...