首页 > 试题广场 >

请编写递归算法查找字符串中相同字符连续出现的最大次数,例aa

[问答题]
请编写递归算法查找字符串中相同字符连续出现的最大次数,例aaabbcc最大连续重复数为3

static int maxConsecutiveRepetition(String str){
        if(str.length()==0){
            return 0;
        }
        int max = 0;
        int count = 1;
        for(int i=0;i<str.length()-1;i++){
            if(str.charAt(i)==str.charAt(i+1)){
                count++;
            }else{
                count=1;
            }
            if(count>max){
                max = count;
            }
        }
        return max;
    }


编辑于 2022-04-08 14:18:36 回复(0)
int CountLengest(char* pBuf)  
{  
    int result=0;  
    int nextLen;
    char *p=pBuf;  
    if(*pBuf=='\0')  
        return result;  
  
    while((*p!='\0') && (*p==*pBuf))  
    {  
        result++;  
        p++;  
    }  
    nextLen=CountLengest(p);
    return result > nextL en  ? result : nextL en ;  
  
}  
发表于 2015-01-14 19:52:11 回复(0)