【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 个 ...
随机推荐
- Makefile:248: /usr/local/otp_src_18.1/make/x86_64-unknown-linux-gnu/otp_ded.mk: No such file
安装erlang的时候,使用make命令一直报这个错 Makefile:248: /usr/local/otp_src_18.1/make/x86_64-unknown-linux-gnu/otp_d ...
- clean()方法的简单应用
clean()方法主要用于验证相互依赖的字段,例如注册时,填写的“密码”和“确认密码”要相等时才符合要求. 在调用表单clean() 方法的时候,所有字段的验证方法已经执行完(表单字段的默认验证(如C ...
- Windows10-Neo4j安装问题及解决方案
暑假都过得差不多了才终于开始搭环境了 1.下载Neo4j Neo4j官网下载翻墙的话还可以 不翻墙的话下了好几次都下不下来 不用下载desktop,下载community server就可以了 2.下 ...
- 05_Tutorial 5: Relationships & Hyperlinked APIs 关系和超链接
1.关系和超链接 0.文档 https://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/ h ...
- element案例大杂烩
修改表头字体粗细? <el-table :data="list" header-row-class-name="tableHead"> 自定义即可 ...
- reCaptcha 新版,国内可无障碍使用
reCaptcha 新版,国内可无障碍使用 蓝小灰 Digital Sign® PKI 创始人/一点安全专栏主编 4 人赞同了该文章 如果你在使用一些网站看到下图,这就是由 Google 提供的 re ...
- 我的公众号 - Old Artist
如果可以,我希望大家关注我的公众号,没有技术,没有华丽的文笔,只有简简单单的语句. 分享一些小的事情,大家好,我是老艺术家. Thanks~
- 使用DOS命令将类库封装成dll
1.Windows键+R.输入cmd进入DOS 2.使用 cd 加路径找到需要封装成dll的类库文件 3.csc /target:library /out:dll的名字.DLL 需要封装的cs文件
- Nut和nuTilda【翻译】
翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/72176-nut-nutilda.html phile: ...
- Arts打卡第6周
Algorithm.主要是为了编程训练和学习. 每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard). 进行编程训练,如果不训练你看再多的算法书,你依然不 ...