当前位置:网站首页>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;
}
边栏推荐
- Sizeof (function name) =?
- Imx6ull bare metal development learning 2- use C language to light LED indicator
- 实例009:暂停一秒输出
- 2022.7.4-----leetcode.1200
- MATLAB小技巧(28)模糊综合评价
- 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?
- Stm32--- systick timer
- Some pitfalls of win10 network sharing
- QEMU STM32 vscode debugging environment configuration
- STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
猜你喜欢
My-basic application 2: my-basic installation and operation
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
【三层架构及JDBC总结】
Brief discussion on Buck buck circuit
Stm32--- systick timer
DokuWiki deployment notes
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
【三层架构】
Array integration initialization (C language)
随机推荐
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
STM32 lights up the 1.8-inch screen under Arduino IDE
leetcode - 445. Add two numbers II
Semiconductor devices (I) PN junction
Briefly talk about the identification protocol of mobile port -bc1.2
Affected tree (tree DP)
实例006:斐波那契数列
One question per day - replace spaces
STM32---IIC
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
List of linked lists
Slist of linked list
【三层架构】
Imx6ull bare metal development learning 2- use C language to light LED indicator
Several important parameters of LDO circuit design and type selection
[paper reading] the latest transfer ability in deep learning: a survey in 2022
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
UE pixel stream, come to a "diet pill"!
Stablq of linked list