当前位置:网站首页>696. 计数二进制子串
696. 计数二进制子串
2022-07-05 08:31:00 【Mr Gao】
696. 计数二进制子串
给定一个字符串 s,统计并返回具有相同数量 0 和 1 的非空(连续)子字符串的数量,并且这些子字符串中的所有 0 和所有 1 都是成组连续的。
重复出现(不同位置)的子串也要统计它们出现的次数。
示例 1:
输入:s = “00110011”
输出:6
解释:6 个子串满足具有相同数量的连续 1 和 0 :“0011”、“01”、“1100”、“10”、“0011” 和 “01” 。
注意,一些重复出现的子串(不同位置)要统计它们出现的次数。
另外,“00110011” 不是有效的子串,因为所有的 0(还有 1 )没有组合在一起。
示例 2:
输入:s = “10101”
输出:4
解释:有 4 个子串:“10”、“01”、“10”、“01” ,具有相同数量的连续 1 和 0 。
这题其实挺复杂,我们需要做很多优化:
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;
}
边栏推荐
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- 实例008:九九乘法表
- 实例007:copy 将一个列表的数据复制到另一个列表中。
- Five design details of linear regulator
- Stm32--- systick timer
- 如何写Cover Letter?
- Design a clock frequency division circuit that can be switched arbitrarily
- [tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
- 实例006:斐波那契数列
- Matlab tips (28) fuzzy comprehensive evaluation
猜你喜欢

Several important parameters of LDO circuit design and type selection

NTC thermistor application - temperature measurement
![[three tier architecture]](/img/73/c4c75a453f03830e83cabb0762eb9b.png)
[three tier architecture]

99 multiplication table (C language)
![[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement](/img/54/78e4e10724b2762274b06debb10877.jpg)
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement

Semiconductor devices (I) PN junction

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

【三层架构及JDBC总结】

More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!

List of linked lists
随机推荐
Sizeof (function name) =?
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
Classic application of MOS transistor circuit design (2) - switch circuit design
UE像素流,来颗“减肥药”吧!
Affected tree (tree DP)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
Use indent to format code
Management and use of DokuWiki (supplementary)
Various types of questions judged by prime numbers within 100 (C language)
2022.7.4-----leetcode. one thousand and two hundred
Summary of SIM card circuit knowledge
MySQL之MHA高可用集群
Negative pressure generation of buck-boost circuit
Synchronization of QT multithreading
STM32 single chip microcomputer - external interrupt
STM32---IIC
Explication de la procédure stockée pour SQL Server
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
MySQL MHA high availability cluster