当前位置:网站首页>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;
}
边栏推荐
- MHA High available Cluster for MySQL
- Sql Server的存儲過程詳解
- STM32 --- GPIO configuration & GPIO related library functions
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- STM32---ADC
- Is the security account given by Yixue school safe? Where can I open an account
- QEMU STM32 vscode debugging environment configuration
- STM32 single chip microcomputer - bit band operation
- STM32---IIC
- Several important parameters of LDO circuit design and type selection
猜你喜欢

Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?

实例010:给人看的时间

【NOI模拟赛】汁树(树形DP)

UE pixel stream, come to a "diet pill"!

Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
![[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl](/img/8a/702019b44c8e60dffbcd898330afcb.png)
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl

Negative pressure generation of buck-boost circuit

Arduino burning program and Arduino burning bootloader

Arduino operation stm32
![[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication](/img/08/7f28008a4aa999650998ba8dee5d8e.jpg)
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
随机推荐
Brief discussion on Buck buck circuit
Circleq of linked list
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
实例008:九九乘法表
leetcode - 445. Add two numbers II
QEMU demo makefile analysis
Void* C is a carrier for realizing polymorphism
Simple design description of MIC circuit of ECM mobile phone
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
Cmder of win artifact
Array integration initialization (C language)
Google sitemap files for rails Projects - Google sitemap files for rails projects
实例007:copy 将一个列表的数据复制到另一个列表中。
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Is the security account given by Yixue school safe? Where can I open an account
[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...)
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
STM32 --- configuration of external interrupt
Sql Server的存储过程详解