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语言之——整型的隐式转换与溢出检测

    溢出检测 我们可以通过下面的方式来检测溢出: if(a > INT_MAX - b) {     printf("overflow\n"); } 我们利用整型的最大值减去其中 ...

  2. 网络编程---scoket使用,七层协议,三次挥手建连接,四次挥手断连接

    目录 == 网络编程 == 软件开发架构 网络编程 互联网协议 TCP协议的工作原理 Socket == 网络编程 == 软件开发架构 开发软件 必须要开发一套 客户端与服务端 客户端与服务端的作用 ...

  3. 基于评分的商品top-N推荐系统

    import io # needed because of weird encoding of u.item file import os from surprise import KNNBaseli ...

  4. Airflow 配置celery+rabbitmq和celery+redis

    Airflow 配置celery+rabbitmq 1.安装celery和rabbitmq组件 pip3 install apache-airflow[celery] pip3 install apa ...

  5. 通过时间戳批量删除hbase的数据

    如何通过时间戳批量删除hbase的数据 我们使用hive关联hbase插入数据时,有时会写错数据,此时hbase中的数据量已经很大很大了(上亿).此时,我们要修改错误的数据,只需要删除写错的那部分数据 ...

  6. 传统PC机I/O位址空间范围

    空间范围是0x000--0x3FF,有1024个I/O端口位址可供使用! 使用EISA或PCI等汇流排结构的现代PC机,有64KB的I/O位址空间可供使用.在普通Linux系统下透过查看/proc/i ...

  7. ckeditor自定义工具栏

    /** * 获取编辑器工具栏自定义参数 * @param type 类型 simple=极简版 basic=基本版 full=完整版 */ function get_ckeditor_toolbar( ...

  8. 为什么会选择redis数据库?

    因为redis支持主从同步,而且数据都是缓存在内存中,所以基于redis的分布式爬虫,对请求和数据的高频读取效率非常高

  9. Java基础系列 - 抽象类继承和接口实现

    package com.inter; /** * 继承和接口的关系,单继承,多接口 * java不支持多继承,但可通过接口实现多重继承 */ public class test2 { public s ...

  10. (转)awk 详解

    出处:https://blog.51cto.com/yijiu/1358416 awk详解 awk是一款非常牛逼的报告生成工具,能够将文本格式化成显示为比较直观的结果 废话不多说,直接上例子 awk的 ...