当前位置:网站首页>696. Count binary substring
696. Count binary substring
2022-07-05 08:35:00 【Mr Gao】
696. Counting binary substrings
Given a string s, Count and return with the same number 0 and 1 Non empty of ( continuity ) Number of substrings , And all of these substrings 0 And all 1 They are continuous in groups .
Recurring ( Different positions ) The number of substrings should also be counted .
Example 1:
Input :s = “00110011”
Output :6
explain :6 The substring satisfies the same number of consecutive 1 and 0 :“0011”、“01”、“1100”、“10”、“0011” and “01” .
Be careful , Some repeated substrings ( Different positions ) Count the number of times they appear .
in addition ,“00110011” Not a valid substring , Because of all the 0( also 1 ) No combination .
Example 2:
Input :s = “10101”
Output :4
explain : Yes 4 Substring :“10”、“01”、“10”、“01” , Continuous with the same number 1 and 0 .
This problem is actually quite complicated , We need to do a lot of optimization :
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;
}
边栏推荐
- Detailed summary of FIO test hard disk performance parameters and examples (with source code)
- Guess riddles (2)
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- 实例009:暂停一秒输出
- STM32 --- GPIO configuration & GPIO related library functions
- STM32 single chip microcomputer - bit band operation
- Simple design description of MIC circuit of ECM mobile phone
- MHA High available Cluster for MySQL
- The first week of summer vacation
- On boost circuit
猜你喜欢

猜谜语啦(10)

Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off

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

DCDC circuit - function of bootstrap capacitor

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

实例006:斐波那契数列

Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)

Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase

Bluebridge cup internet of things competition basic graphic tutorial - clock selection

Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
随机推荐
Lori remote control LEGO motor
实例009:暂停一秒输出
99 multiplication table (C language)
Arduino+a4988 control stepper motor
Arduino operation stm32
GEO数据库中搜索数据
如何写Cover Letter?
Esp8266 interrupt configuration
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Five design details of linear regulator
One question per day - replace spaces
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Typical low code apaas manufacturer cases
DCDC circuit - function of bootstrap capacitor
Low code platform | apaas platform construction analysis
Lori remote control commissioning record
实例008:九九乘法表
Briefly talk about the identification protocol of mobile port -bc1.2
QEMU STM32 vscode debugging environment configuration
MySQL MHA high availability cluster