题解 | #字符串的相邻字符去重#

字符串的相邻字符去重

https://www.nowcoder.com/practice/8c7d55263c624faa8e48a16d92c2e90d

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return string字符串
     */
    string removeDuplicates(string s) {
        // write code here

        stack<char> charstack;


        for(int i=0; i<s.size(); i++){
            if(charstack.empty())
            {
                charstack.push(s[i]);
            }else if(!charstack.empty()){
                if(s[i] == charstack.top()){
                    charstack.pop();
                }else{
                    charstack.push(s[i]);
                }
            }
        }


        string s_r;


        for(int i= charstack.size()-1; i>=0; i--){
          
            cout<<s[i]<<endl;

            s[i] = charstack.top();

            s_r.insert(0, 1, s[i]);
            charstack.pop();
      
        }    
        


        return s_r;

    }
};

全部评论

相关推荐

06-04 16:50
腾讯_TEG_技术
点赞 评论 收藏
分享
牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务