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:

  1. Input: "00110011"
  2. Output: 6
  3. Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".

  4. Notice that some of these substrings repeat and are counted the number of times they occur.

  5. Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.

Example 2:

  1. Input: "10101"
  2. Output: 4
  3. Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1'
  4.  
  5. 题意:给定一个由0和1组成的非空字符串,计算出由相同数量的0和1、且0和1分别连续的子串的个数。
    思路1:直接暴力解决,但时间复杂度高,提交后time limit exceeded。从前往后遍历,判断当前数字的连续相同数字的数量,再找出从第一个不同数字开始,连续不同数字的数量,数量相同则count加1,否则count不变。
    代码如下:
  1. public int countBinarySubstrings(String s) {
  2. char[] chars = s.toCharArray();
  3. int count = 0;
  4. for(int i = 0; i < chars.length - 1; i++){
  5. if(isSubstrings(chars, i)){
  6. count++;
  7. }
  8. }
  9. return count;
  10. }
  11. public boolean isSubstrings(char[] chars, int start){
  12. int same = start + 1;
  13. while(same < chars.length - 1 && chars[start] == chars[same]){
  14. same++;
  15. }
  16. int diff = same;
  17. while(diff < chars.length && chars[diff] != chars[start] && diff - same < same - start){
  18. diff++;
  19. }
  20. return diff - same == same - start ? true : false;
  21. }

思路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,所以不可以再组成满足条件的子串了。

代码如下:

  1. public int contBinarySubstrings(String s){
  2. char[] chars = s.toCharArray();
  3. int[] group = new int[chars.length];
  4. int index = 0;
  5. group[0] = 1;
  6. for(int i = 1; i < chars.length; i++){
  7. if(chars[i] == chars[i - 1])
  8. group[index]++;
  9. else
  10. group[++index] = 1;
  11. }
  12. int i = 0, count = 0;
  13. while(i < group.length - 1 && group[i] != 0){
  14. count += Math.min(group[i], group[i + 1]);
  15. i++;
  16. }
  17. return count;
  18. }

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

代码如下:

  1.  
  1. public int contBinarySubstring(String s){
  2. int pre = 0, cur = 1, count = 0;
  3. for(int i = 1; i < s.length(); i++){
  4. if(s.charAt(i) != s.charAt(i - 1)){
  5. count += Math.min(pre, cur);
  6. pre = cur;
  7. cur = 1;
  8. }
  9. else{
  10. cur++;
  11. }
  12. }
  13. return count + Math.min(pre, cur);
  14. }
  1.  
  1.  

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. 学号 2016-2017-20155329《Java程序设计》课程总结

    学号 2016-2017-20155329<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:想象中的师生关系 预备作业2:C语言水平调查以及认为自己最强的一项技能和毕业 ...

  2. MSP-EZ430U_02板子测试使用

    1. 实物如下 2. 先上电,显示驱动没安装 3. 找到驱动的位置,不过实际上安装IAR for msp430之后,驱动就自动的识别了.

  3. 【python笔记】python中的list、tuple、set、dict用法简析

    list list是一种有序的集合(或称作列表),可以很方便地添加和删除其中的元素. >>> classmates = ['Michael', 'Bob', 'Tracy'] 可通过 ...

  4. 质造未来,首届腾讯WeTest技术交流开放日成功举办

    WeTest 导读 北京时间12月21日下午1点整,2018年度腾讯WeTest技术交流开放日在上海举办.盛大.巨人.电魂.bilibili.方趣等十余家来自不同优秀企业的测试技术负责人均来到现场,共 ...

  5. javaweb(二十)——JavaBean总结

    一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...

  6. windows下如何将Python文件打包成.exe可执行文件

    在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电脑后就没法运行了.这显得很不方便,不符合我们的初衷,那么有没 ...

  7. JavaScript 中函数的参数

    functionName(parameter1, parameter2, parameter3) { // 要执行的代码…… } 参数规则 JavaScript 函数定义时形参没有指定数据类型. Ja ...

  8. selenium自动化之稳定版本环境介绍

    大家都知道,目前selenium版本已经升级到3.0了,selenium3只是在selenium2的基础上做了一些调整,最明显的区别就是 selenium2对Firefox的支持最高只支持46及以下版 ...

  9. [Ubuntu] <uptime>命令

    uptime 命令 就是查看系统启动时间的,前几个大家应该都很熟悉:当前时间.系统启动时间.正在登陆的用户数 最后的三个数字,分别代表过去 1分钟  5分钟  15分钟  的平均负载(Load Ave ...

  10. 常用SQL语句大全(SQL Server)

    一.基础       查看数据库状态 select state_desc from sys.databases where name='dbname'  -- dbname数据库名 1.说明:创建数据 ...