zoukankan      html  css  js  c++  java
  • 696. Count Binary Substrings统计配对的01个数

    [抄题]:

    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.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    以为要用stack,其实是文字游戏

    [一句话思路]:

    相等时统计长度,不相等时断绝关系

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    用prev curt即可判断配对

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    1. 打草稿的时候,框架写在for的里面
    class Solution {
        public int countBinarySubstrings(String s) {
            //cc
            if (s.length() == 0) {
                return 0;
            }
            //ini
            int curLen = 1, preLen = 0, res = 0;
            //for loop: for i to n - 1
            //stop count or not
            for (int i = 1; i < s.length(); i++) {
                if (s.charAt(i) == s.charAt(i - 1)) {
                    curLen++;
                }else {
                    preLen = curLen;
                    curLen = 1;
                }
                //add res or not
            if (preLen >= curLen) {
                res++;
            }
            }
            
            //return res
            return res;
        }
    }
    View Code
  • 相关阅读:
    JavaScript---动态加载script和style样式
    百度地图API----搜索地址,获取该点的位置坐标并转换成WebMercator
    ArcGIS api for JavaScript 3.27 FindTask查询功能
    Dojo.declare使用方法详解
    ArcGIS Desktop中为图像设置成不显示背景色
    网页导航条定位
    css3+JS实现幻灯片轮播图
    简单json---转树形json
    js中回调函数写法
    pom.xml文件报MavenArchiver错误 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8639332.html
Copyright © 2011-2022 走看看