第二题写半天才A,还觉得自己方法很low,求大佬高效做法

class Solution {
public:
    /**
     * 
     * @param str string字符串 初始字符串
     * @return string字符串
     */
    stack<char> stack;
    string solve(string str) {
        // write code here
        for(int i=0; i<str.size(); i++) {
            if(stack.empty()) {stack.push(str[i]); continue;}
            
            if(str[i] != stack.top()) stack.push(str[i]);
            else {
                if(str[i] == '0') {
                    stack.pop();
                    if(stack.empty())   stack.push('1');
                    else  stack.pop();
                }
                else stack.pop();
            }
        }
        string res;
        while(!stack.empty())
        {
            res += stack.top();
            stack.pop();
        }
        reverse(res.begin(), res.end());
        return res;
    }
};


全部评论
https://ac.nowcoder.com/acm/contest/view-submission?submissionId=44686469
点赞 回复 分享
发布于 2020-08-14 10:28

相关推荐

求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
牛客517626884号:嵌入式真难啊今年,我电赛国二都成了路边野狗了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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