怕npy的牛牛
- 双指针解题
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 返回符合题意的最长的子串长度
* @param x string字符串
* @return int整型
*/
int Maximumlength(string x) {
// write code here
int n = 0, p = 0, y = 0;
int ans = 0;
for(int i = 0, j = 0; j < x.size(); ++j)
{
if(x[j]=='n') n++;
else if(x[j]=='p') p++;
else if(x[j]=='y') y++;
while((n>0 && p>0 && y>0))
{
if(x[i]=='n') n--;
else if(x[i]=='p') p--;
else if(x[i]=='y') y--;
i++;
}
ans = max(ans, j-i+1);
}
return ans;
}
};
深信服公司福利 845人发布