当前位置:网站首页>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;
}
边栏推荐
- Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
- [three tier architecture and JDBC summary]
- FIO测试硬盘性能参数和实例详细总结(附源码)
- 亿学学堂给的证券账户安不安全?哪里可以开户
- 每日一题——替换空格
- Business modeling of software model | vision
- 猜谜语啦(9)
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- Apaas platform of TOP10 abroad
- Low code platform | apaas platform construction analysis
猜你喜欢
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Typical low code apaas manufacturer cases
Lori remote control LEGO motor
MySQL MHA high availability cluster
Business modeling of software model | stakeholders
猜谜语啦(6)
Meizu Bluetooth remote control temperature and humidity access homeassistant
One question per day - replace spaces
STM32---IIC
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
随机推荐
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Brief discussion on Buck buck circuit
Sword finger offer 05 Replace spaces
图解八道经典指针笔试题
Daily question - input a date and output the day of the year
暑假第一周
L298N module use
STM32 outputs 1PPS with adjustable phase
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
QEMU demo makefile analysis
Negative pressure generation of buck-boost circuit
Installation and use of libjpeg and ligpng
MATLAB小技巧(28)模糊綜合評價
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Charge pump boost principle - this article will give you a simple understanding
Example 009: pause output for one second
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?