原题链接在这里: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.length will be between 1 and 50,000.
  • s will 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;
}
}

类似Encode and Decode Strings.

LeetCode Count Binary Substrings的更多相关文章

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

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

  2. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  3. 696. Count Binary Substrings - LeetCode

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

  4. 【Leetcode_easy】696. Count Binary Substrings

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

  5. LeetCode 696. Count Binary Substrings

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

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

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

  7. LeetCode算法题-Count Binary Substrings(Java实现)

    这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...

  8. LeetCode 696 Count Binary Substrings 解题报告

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

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

随机推荐

  1. iOS JS 和 OC交互 / JS 和 native 相互调用

    现在app 上越来越多需求是通过UIWebView 来展示html 或者 html5的内容, js 和 native OC代码交互 就非常常见了. js 调用 native  OC代码 第一种机制 ( ...

  2. windows7 安装Apache2时出现failed to open the winNT service manager 提示

    因为电脑实在太慢了,C盘的空间所剩无几,要想再安装大一点的软件的话,可能性很小.加之系统已经好久没有重装过了,于是重新安装windows7旗舰版,系统装好后,免不了一堆软件的重装和开发环境配置,首要的 ...

  3. Java 内部类、静态类内部类

    问: 什么是内部类? 答: 内部类(Inner Class)就是在一个类的内部再定义一个类,与之对应包含内部类的类被称为外部类. 问: 为什么要将一个类定义在另外一个类内部呢? 答: 内部类主要作用如 ...

  4. Go 功能测试与性能测试

    1.功能测试 calcTriangle.go // 需要被测试的函数 func calcTriangle(a, b int) int { return int(math.Sqrt(float64(a* ...

  5. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component...

    今天开发犯了一个特lowB的错,记录下来,引以为戒! 严重: A child container failed during start java.util.concurrent.ExecutionE ...

  6. 【bzoj2151】种树(堆/优先队列+双向链表)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2151 这道题因为优先队列不怎么会用,而且手写堆的代码也不长,也想复习一下手写堆的写法…… ...

  7. c# 判断字符串中是否含有汉字,数字

    正则表达式使用时需要引用 using System.Text.RegularExpressions; private void buttonX1_Click(object sender, EventA ...

  8. Excel 如何复制粘贴一整行

    在某些时候,我们需要重复性的录入一些信息,而且表头都是一样的,一直拉列宽是相当令人烦躁的事情. 所以,就想可以直接复制黏贴出一整行,包括行的各个列宽. 具体操作如图: ——>首先,一整行复制, ...

  9. 比较好的sql写法

    DECLARE @beginTime VARCHAR(20)= '2017-11-13 00:00:00';DECLARE @endTime VARCHAR(20)= '2017-11-13 23:0 ...

  10. BZOJ2877 [Noi2012]魔幻棋盘

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...