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
  • 相关阅读:
    ArcGIS数据建模 (模型构建器modelbuilder) 培训视频 5章28小节587分钟视频 51GIS网站上线
    arcgis python ListEnvironments 函数可返回地理处理环境名称列表。
    arcgis python ValueTable使用
    解决ArcMap启动时只停留在初始化界面的方法
    Eutils用法总结
    EF 汇总函数使用注意事项Max()/Min()等
    C#多线程
    EF Attach时报错
    [Windows报错]要求的函数不受支持、这可能是由于 CredSSP 加密 Oracle 修正
    C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8639332.html
Copyright © 2011-2022 走看看