[抄题]:

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;
}
}

最新文章

  1. 餐厅点餐系统app总结
  2. 在JSP中使用JavaBean
  3. yii2 sphinx Ajax搜索分页 关键词的缓存
  4. 搭建Asp.Net MVC4
  5. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)
  6. python爬虫--自动获取seebug的poc
  7. python 三级联动
  8. RfcConfig 类 主要解决Tomcat 报 The valid characters are defined in RFC 7230 and RFC 3986
  9. 【JavaScript】 使用extend继承对象的prototype方法
  10. 异常: Bean named &#39;org.springframework.transaction.interceptor.TransactionInterceptor#0&#39; is expected to be of type &#39;org.aopalliance.aop.Advice&#39; but was actually of type &#39;org.springframework.transaction.i
  11. Linux大页内存管理等---菜鸟初学
  12. 35 个最好用的 Vue 开源库
  13. Linux学习笔记--vim
  14. 【vue】如何在 Vue-cli 创建的项目中引入iView
  15. python中高阶函数与装饰器(2)
  16. 【转】JS函数集合大全
  17. API To Import Negotiations(转)
  18. C++实现顺序查找,折半查找,插值查找
  19. Consul ACL
  20. 什么是Apache Isis

热门文章

  1. I.MX6 PHY fixup 调用流程 hacking
  2. WCF 配置文件
  3. 【eclipse】 怎么解决java.lang.NoClassDefFoundError错误
  4. JAVA框架--hibernate、struts2、spring
  5. C#网络编程(订立协议和发送文件) - Part.4
  6. ③SpringBoot中Redis的使用
  7. idea操作
  8. ubuntu为什么没有/etc/inittab文件? 深究ubuntu的启动流程分析
  9. VirtualBox 虚拟机 centos7 下 设置静态ip 并支持 xshell 远程登陆的设置方法
  10. MySQL写出高效SQL