当前位置:网站首页>696. 计数二进制子串
696. 计数二进制子串
2022-07-05 08:31:00 【Mr Gao】
696. 计数二进制子串
给定一个字符串 s,统计并返回具有相同数量 0 和 1 的非空(连续)子字符串的数量,并且这些子字符串中的所有 0 和所有 1 都是成组连续的。
重复出现(不同位置)的子串也要统计它们出现的次数。
示例 1:
输入:s = “00110011”
输出:6
解释:6 个子串满足具有相同数量的连续 1 和 0 :“0011”、“01”、“1100”、“10”、“0011” 和 “01” 。
注意,一些重复出现的子串(不同位置)要统计它们出现的次数。
另外,“00110011” 不是有效的子串,因为所有的 0(还有 1 )没有组合在一起。
示例 2:
输入:s = “10101”
输出:4
解释:有 4 个子串:“10”、“01”、“10”、“01” ,具有相同数量的连续 1 和 0 。
这题其实挺复杂,我们需要做很多优化:
int countBinarySubstrings(char * s){
int zerocount=0,onecount=0;
int i;
int count=0;
while(s[i]!='\0'){
int j;
for(j=i;s[j]!='\0';j++){
// printf("--%d %d ",zerocount,onecount);
if(s[j]=='0'){
zerocount++;
if(onecount!=0&&zerocount>onecount){
i=i+zerocount-onecount-2;
onecount=0;
zerocount=0;
break;
}
if(zerocount==onecount){
count=count+zerocount;
i=i+zerocount-1;
onecount=0;
zerocount=0;
break;
}
}
if(s[j]=='1'){
onecount++;
if(zerocount!=0&&onecount>zerocount){
i=i+onecount-zerocount-2;
onecount=0;
zerocount=0;
break;
}
if(zerocount==onecount){
count=count+zerocount;
i=i+zerocount-1;
onecount=0;
zerocount=0;
break;
}
}
}
onecount=0;
zerocount=0;
i++;
}
return count;
}
边栏推荐
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
- Example 009: pause output for one second
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- How to copy formatted notepad++ text?
- Buildroot system for making raspberry pie cm3
- Hardware and software solution of FPGA key chattering elimination
- UE像素流,来颗“减肥药”吧!
- [trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
- Go dependency injection -- Google open source library wire
- [trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
猜你喜欢

MATLAB小技巧(28)模糊综合评价
![[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication](/img/05/0f63e4cd3da24e5b956ec5899b939d.jpg)
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication

Detailed summary of FIO test hard disk performance parameters and examples (with source code)

99 multiplication table (C language)

List of linked lists

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.

实例010:给人看的时间

Example 008: 99 multiplication table
![[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)](/img/54/2fe86f54af01f10de93818103f2154.jpg)
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
随机推荐
2022.7.4-----leetcode. one thousand and two hundred
2022.7.4-----leetcode.1200
MySQL之MHA高可用集群
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
STM32---ADC
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
MySQL MHA high availability cluster
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
PIP installation
L298N module use
STM32 --- serial port communication
Detailed explanation of SQL server stored procedures
STM32 virtualization environment of QEMU
Example 006: Fibonacci series
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
STM32---IIC
Sword finger offer 06 Print linked list from end to end
实例010:给人看的时间
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Slist of linked list