problem

696. Count Binary Substrings

题意:具有相同个数的1和0的连续子串的数目;

solution1:还不是特别理解。。。

遍历元数组,如果是第一个数字,那么对应的ones或zeros自增1。然后进行分情况讨论,如果当前数字是1,然后判断如果前面的数字也是1,则ones自增1,否则ones重置为1。如果此时zeros大于ones,res自增1。反之同理,如果当前数字是0,然后判断如果前面的数字也是0,则zeros自增1,否则zeros重置为1。如果此时ones大于zeros,res自增1。

class Solution {
public:
int countBinarySubstrings(string s) {
int zeros = , ones = , res = ;
for(int i=; i<s.size(); ++i)
{
if(i==) s[i]=='' ? zeros++ : ones++;
else if (s[i]=='')
{
ones = (s[i-]=='') ? ones+ : ;
if(zeros>=ones) res++;
}
else if(s[i]=='')
{
zeros = (s[i-]=='') ? zeros+ : ;
if(ones>=zeros) res++;
}
}
return res;
}
};

solution2:

class Solution {
public:
int countBinarySubstrings(string s) {
int pre = , cur = , res = ;
for(int i=; i<s.size(); ++i)
{
if(s[i]==s[i-]) cur++;
else
{
pre = cur;
cur = ;
}
if(pre>=cur) res++;
}
return res;
}
};

参考

1. Leetcode_easy_696. Count Binary Substrings;

2. Grandyang;

【Leetcode_easy】696. Count Binary Substrings的更多相关文章

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

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

  2. 696. Count Binary Substrings - LeetCode

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

  3. LeetCode 696 Count Binary Substrings 解题报告

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

  4. 696. Count Binary Substrings

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

  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. LeetCode 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_easy】965. Univalued Binary Tree

    problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完

  9. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

随机推荐

  1. PL/SQL复合类型

    一.PL/SQL记录:一条记录. 可简化单行多列的数据的处理.当使用pl/sql记录时,应用开发人员即可以自定义记录类型和记录变量,也可以使用%rowtype属性直接定义记录变量. 1.当使用自定义的 ...

  2. NAT和PAT

    地址转换技术 优点: 内网能够主动访问外网 外网不能主动访问内网 内网安全 节省公网ip地址 缺点:慢   PAT 端口地址转换 节省公网IP 替换源端口和源地址 NAT 不节省公网IP 一个公网地址 ...

  3. linux中的set ff=unix

    set ff=unix : 告诉 vi 编辑器,使用unix换行符. 操作步骤: 1.用vi命令打开文件 2.直接输入 :set ff=unix

  4. ORA-00600[2662]问题 汇总

    一.ORA-00600[2662]问题模拟及解决方法 这是2013年的一篇文章,也不知道数据库是什么版本, 我的数据库时11.2.0.4,使用下面方法模拟的时候,模拟不出来....   参照eygle ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Input类中常用的验证方式

    Deolin一般将Input类对象作为POST请求方法的参数, Input类的域与前端的数据结构一一对应,由于后端不应该相信前端传过来的任何数据, 所以前端的数据对象先绑定到Input对象中,通过JS ...

  7. 使用dig进行DNS查询

    dig全称Domain Information Groper,是一个DNS域名信息查询的工具,可以使用来查看域名解析的过程. dig是linux下自带的工具,如果要在windows下使用需要自行下载和 ...

  8. BigDecimal进行除法运算时的坑

      循环小数输出的坑 BigDecimal做除法时如果出现除不尽(循环小数)的情况,会抛异常: BigDecimal a = new BigDecimal("1"); System ...

  9. 【转】gcc的__builtin_函数介绍

    转自:http://blog.csdn.net/jasonchen_gbd/article/details/44948523 GCC提供了一系列的builtin函数,可以实现一些简单快捷的功能来方便程 ...

  10. [PKUSC2018]真实排名——线段树+组合数

    题目链接: [PKUSC2018]真实排名 对于每个数$val$分两种情况讨论: 1.当$val$不翻倍时,那么可以翻倍的是权值比$\frac{val-1}{2}$小的和大于等于$val$的. 2.当 ...