当前位置:网站首页>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;
}
边栏推荐
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Run菜单解析
- STM32 virtualization environment of QEMU
- 实例010:给人看的时间
- Guess riddles (2)
- 实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
- 每日一题——输入一个日期,输出它是该年的第几天
- Low code platform | apaas platform construction analysis
- 【NOI模拟赛】汁树(树形DP)
- go依赖注入--google开源库wire
猜你喜欢

Typical low code apaas manufacturer cases
![[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl](/img/8a/702019b44c8e60dffbcd898330afcb.png)
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl

Various types of questions judged by prime numbers within 100 (C language)

Example 006: Fibonacci series

剑指 Offer 05. 替换空格

猜谜语啦(7)

Business modeling | process of software model

Meizu Bluetooth remote control temperature and humidity access homeassistant

An enterprise information integration system

Bluebridge cup internet of things competition basic graphic tutorial - clock selection
随机推荐
Talk about the circuit use of TVs tube
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Arduino+a4988 control stepper motor
Old Wang's esp8266 and old Wu's ws2818 light strip
STM32 virtualization environment of QEMU
How apaas is applied in different organizational structures
Classic application of MOS transistor circuit design (2) - switch circuit design
Affected tree (tree DP)
Count the number of inputs (C language)
Cmder of win artifact
Array integration initialization (C language)
STM32 single chip microcomputer - external interrupt
Talk about the function of magnetic beads in circuits
Arduino burning program and Arduino burning bootloader
猜谜语啦(9)
关于线性稳压器的五个设计细节
C语言标准函数scanf不安全的原因
Simple design description of MIC circuit of ECM mobile phone
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Illustration of eight classic pointer written test questions