当前位置:网站首页>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;
}
边栏推荐
- Example 007: copy data from one list to another list.
- STM32 single chip microcomputer - external interrupt
- 第十八章 使用工作队列管理器(一)
- 【三层架构及JDBC总结】
- Lori remote control LEGO motor
- Keil use details -- magic wand
- One question per day - replace spaces
- Arduino burning program and Arduino burning bootloader
- 2022.7.4-----leetcode. one thousand and two hundred
- Wheel 1:qcustomplot initialization template
猜你喜欢
随机推荐
Typical low code apaas manufacturer cases
Summary of SIM card circuit knowledge
Cmder of win artifact
Explore the authentication mechanism of StarUML
Sword finger offer 06 Print linked list from end to end
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
STM32 --- NVIC interrupt
Low code platform | apaas platform construction analysis
319. 灯泡开关
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
DCDC circuit - function of bootstrap capacitor
Business modeling of software model | stakeholders
Various types of questions judged by prime numbers within 100 (C language)
猜谜语啦(6)
Talk about the function of magnetic beads in circuits
Search data in geo database
Count the number of inputs (C language)
Briefly talk about the identification protocol of mobile port -bc1.2
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
C language data type replacement









