首页 > 牛客编程巅峰赛S2赛季第5场代码第二题
头像
投简历⇆面试
编辑于 2020-12-01 21:31
+ 关注

牛客编程巅峰赛S2赛季第5场代码第二题

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 返回符合题意的最长的子串长度
* @param x string字符串
* @return
int整型
*/
int Maximumlength(string x) {
// write code here
int a[3]={0};
queue<char> q;
int max_s=0;
for(int i=0;i<x.size();i++){
q.push(x[i]);
if(x[i]=='n') a[0]++;
else if(x[i]=='p') a[1]++;
else if(x[i]=='y') a[2]++;
while(!q.empty()&&a[0]&&a[1]&&a[2]){
if(q.front()=='n') a[0]--;
else if(q.front()=='p') a[1]--;
else if(q.front()=='y') a[2]--;
q.pop();
}
max_s=max_s>=q.size()?max_s:q.size();
}
return max_s;
}
};

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

近期精华帖

热门推荐