【Leetcode_easy】696. Count Binary Substrings
problem
题意:具有相同个数的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的更多相关文章
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
- 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [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 ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- 【Leetcode_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
随机推荐
- appium+python+iOS 环境搭建,安装fbsimctl
(1)设置postBuffer的值 开始如下的安装之前,需先修改postBuffer的值,默认的比较下,会导致下载大文件时失败, 参考链接:http://blog.sina.com.cn/s/blog ...
- SpringBoot项目mysql配置文件密码加密(jasypt)
起因:因为我个人微博想要公开源码,但数据库配置文件会暴露在外面,又不想生产跟开发环境建来回切换,所以想到了加密数据库密码,于是问了群里的一个朋友小XX(原谅我不识字) 经过: 简单粗暴,直接上代码: ...
- java中使用redis --- Set集合的简单应用
1.java代码 public class RedisTest01 { public static void main(String[] args){ // connect redis server ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- BZOJ3678 wangxz与OJ (平衡树 无旋treap)
题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...
- 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom 题解
每日一题 day11 打卡 Analysis 好久没大Tarjan了,练习练习模板. 只要在Tarjan后扫一遍si数组看是否大于1就好了. #include<iostream> #inc ...
- Ubuntu输入ifconfig找不到IP地址,只有lo问题
首先我的问题是这样的 然后一顿神百度,要找到你之前用的网卡或者设备,ifconfig -a,然后好用,出来了 但是并没有IP地址什么的,网上给的重启命令也没有用 输入的命令是:ifconfig ens ...
- 禁止打印调用(python)
原文 : https://cloud.tencent.com/developer/ask/188486 import os, sys class HiddenPrints: def __enter__ ...
- Lua 常用函数 一
lua_getallocf lua_Alloc lua_getallocf (lua_State *L, void **ud); 返回给定状态机的内存分配器函数.如果 ud 不是 NULL ,Lua ...
- python 最小二乘 leastsq 函数实现 法线式 解决与x轴垂直问题
当使用y=kx+b时,与x轴垂直的直线无法计算.因此使用法线式ysin(theta)+xcos(theta) = dist.貌似这么用有点复杂了,直接使用ax+by=1不知道能不能计算,未测试. # ...