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. C#格式化信息,格式化数字、格式化日期

    一.格式化方法: 1.ToString()实例方法 使用当前文化: varname.ToString("C4"); 使用特定文化: varname.ToString("C ...

  2. Java基础面试题1

    http://blog.csdn.net/jackfrued/article/details/44921941 1.面向对象的特征有哪些方面?  答:面向对象的特征主要有以下几个方面: - 抽象:抽象 ...

  3. 利用python中的库文件简单的展示mnist 中的数据图像

    import sys, os sys.path.append('F:\ml\DL\source-code') #导入此路径中 from dataset.mnist import load_mnist ...

  4. vue 对象更改检测注意事项

  5. 推荐python入门书籍(爬虫方面)

    学爬虫,需要理论与实践相结合,Python生态中的爬虫库多如牛毛,urllib.urllib2.requests.beautifulsoup.scrapy.pyspider都是爬虫相关的库,但是如果没 ...

  6. CSS定位有几种?分别描述其不同

    static(静态定位):即默认值,元素框正常生成的,top.right.bottom.left这些偏移属性不会影响静态定位的正常显示(属性不应用): relative(相对定位):元素相对自身偏移某 ...

  7. CSP模拟赛 Matrix(DP)

    题面 求出满足以下条件的 n*m 的 01 矩阵个数: (1)第 i 行第 1~li 列恰好有 1 个 1. (2)第 i 行第 ri~m 列恰好有 1 个 1. (3)每列至多有 1 个 1. n, ...

  8. 对象(面向对象、创建对象方式、Json)

    一.面向对象 面向过程:凡事亲力亲为,每件事的具体过程都要知道,注重过程 面向对象:根据需求寻找对象,所有的事都用对象来做,注重结果 面向对象特性:封装.继承.多态(抽象性) js是一门基于对象的语言 ...

  9. linux 安装Apache服务器

    这篇文章先别看,,有些地方我不是很明白,写错了一些地方,正在咨询会linux的大神 安装好Apache就可以用Http访问或者下载电脑的文件了 我还是用 连接我的linux电脑 咱把Apache安装到 ...

  10. Jenkins automate workflow

    Now we will build an automate flow from code compiling to product delivery.The essential tools using ...