字串------找出01串中o和1連續出現的最大次數
阿新 • • 發佈:2018-12-09
題目:
思路:
- 記錄遍歷中的最大值,與temp比較,並交換
程式碼:
void Calculate(const char *str,int *max0,int *max1) { int temp0=0; //儲存0串的最大長度 int temp1=0; //儲存1串的最大長度 while(*str) { if(*str=='0') { (*max0)++; //0的長度 if(*str++ == '1') //如果下一個是1 { if(temp0<*max0) //判斷當前長度是否需要儲存 { temp0=*max0; } *max0=0; } } else if(*str=='1') { (*max1)++; //1的長度 if(*str++ == '0') //如果下一個是1 { if(temp1<*max1) //判斷當前長度是否需要儲存 { temp1=*max1; } *max1=0; } } } *max0=temp0; *max1=temp1; }