当前位置:网站首页>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;
}
边栏推荐
- Affected tree (tree DP)
- Stm32--- systick timer
- 轮子1:QCustomPlot初始化模板
- Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
- 实例007:copy 将一个列表的数据复制到另一个列表中。
- How can fresh students write resumes to attract HR and interviewers
- Lori remote control LEGO motor
- Go dependency injection -- Google open source library wire
- STM32 --- NVIC interrupt
- MATLAB小技巧(28)模糊綜合評價
猜你喜欢
STM32---ADC
Sword finger offer 09 Implementing queues with two stacks
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Business modeling of software model | vision
【三层架构及JDBC总结】
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Simple design description of MIC circuit of ECM mobile phone
L298N module use
UE像素流,来颗“减肥药”吧!
QEMU STM32 vscode debugging environment configuration
随机推荐
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
亿学学堂给的证券账户安不安全?哪里可以开户
Keil use details -- magic wand
696. 计数二进制子串
Illustration of eight classic pointer written test questions
Business modeling of software model | vision
Xrosstools tool installation for X-Series
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
每日一题——输入一个日期,输出它是该年的第几天
MHA High available Cluster for MySQL
每日一题——替换空格
Example 008: 99 multiplication table
【三层架构及JDBC总结】
PIP installation
MATLAB小技巧(28)模糊综合评价
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Wheel 1:qcustomplot initialization template
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
整形的分类:short in long longlong
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?