一、代码思路
1、最长平衡子是由零一组成的偶数串,被踢我采用一个空间为二的数组来记录0,1的个数
2、重点是判断0的位置是否位于下一个字串开头(原串首位是0,或该位置前一位是1)。此时,将数组0位置设为一,1位值设为零。否则,读0时,0位自加,读一时,1为自加。
二、代码实现
int findTheLongestBalancedSubstring(char * s){
int n =strlen(s);
int count[2] ={0};
int temp = 0;
for(int i =0;i<n;i++)
{
if((i==0||s[i-1]=='1')&&s[i]=='0')
{
count[0] = 1;
count[1] = 0;
}
else if(s[i]=='0')
{
count[0]++;
}
else
count[1]++;
temp = fmax(temp,2*fmin(count[0],count[1]));//每次保留最大
}
return temp;
}