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; } };
全部评论
(1) 回帖