题目如:abbbaac
消除结果是c
因为leetcode也没有刷题链接,所以自己写了几个测试用例通过了一下,以下是代码:
string xiaoxiaole(string s){
if(s.length()<=2) return s;
stack<char> save;
save.push(s[0]);
int count=1;
int i=1;
string res = "";
while(i<s.length()){
if(!save.empty() && save.top()==s[i]){
count++;
int id = i+1;
while(id<s.length() && s[id]==s[i]) {
count ++;
id++;
}
if(count>=3){//说明连上了,要消除
while(!save.empty() && save.top()==s[i]){
save.pop();
}
if(!save.empty())
count = 1;
else
count = 0;
i=id;
}
}else{
save.push(s[i]);
count = 1;
i++;
}
}
while(!save.empty()){
res=res+string(1,save.top());
save.pop();
}
return res;
}

博客探讨了如何解决一个类似消消乐的编程问题,即消除字符串中连续三个或以上相同的字符。作者提供了自己编写的通过测试用例的代码来实现这一功能。

462

被折叠的 条评论
为什么被折叠?



