Finding consecutive bit string of 1 or 0

前端 未结 10 677
刺人心
刺人心 2020-11-29 05:27

How to find the length of the longest consecutive bit string(either 1 or 0)?

00000000 11110000 00000000 00000000 -> If it is 0 then length will be 20

11111

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 06:21

    Since you didn't wrote what is bit string (regular int, byte array or char string I've assumed that it's char array

    int maxConsBits(char *pStr,char cChar)
    {
        char curChar;
        int curMax = 0;
        int max = 0;
        while (pStr)
        {
           if (*pStr == cChar)
           {
              curMax++;
              if (curMax > max)
              {
                 max = curMax;
              }
           }
           else
           {        
               curMax = 0;
           }
           pStr++;
       }
       return max;
    }
    

提交回复
热议问题