当前位置:网站首页>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;
}
边栏推荐
- 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
- go依赖注入--google开源库wire
- 亿学学堂给的证券账户安不安全?哪里可以开户
- STM32---IIC
- 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?
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- Installation and use of libjpeg and ligpng
- Various types of questions judged by prime numbers within 100 (C language)
- 【三层架构】
猜你喜欢
Arduino uses nrf24l01+ communication
QEMU STM32 vscode debugging environment configuration
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
[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...)
Keil use details -- magic wand
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Count the number of inputs (C language)
OC and OD gate circuit
Working principle and type selection of common mode inductor
随机推荐
List of linked lists
STM32 single chip microcomputer - bit band operation
Synchronization of QT multithreading
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
STM32 outputs 1PPS with adjustable phase
亿学学堂给的证券账户安不安全?哪里可以开户
Imx6ull bare metal development learning 2- use C language to light LED indicator
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
Esphone retrofits old fans
Affected tree (tree DP)
Briefly talk about the identification protocol of mobile port -bc1.2
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Matlab tips (28) fuzzy comprehensive evaluation
MySQL之MHA高可用集群
Meizu Bluetooth remote control temperature and humidity access homeassistant
Arduino+a4988 control stepper motor
实例008:九九乘法表
STM32 --- GPIO configuration & GPIO related library functions
Sql Server的存儲過程詳解