美文网首页
844. Backspace String Compare

844. Backspace String Compare

作者: 张小盒small | 来源:发表于2019-08-04 19:42 被阅读0次
class Solution {
public:
    bool backspaceCompare(string S, string T) {
        stack<char> first;
        stack<char> second;
        for (auto item : S){
            if (!first.empty() && item == '#')
                first.pop();
            if (item != '#')
                first.push(item);
        }
        
        for (auto item : T){
            if (!second.empty() && item == '#')
                second.pop();
            if (item != '#')
                second.push(item);
        }
        return first == second;//判断栈是否相等,此处只是用于判断其中的元素是否全部相等
    }
  
};

相关文章

网友评论

      本文标题:844. Backspace String Compare

      本文链接:https://www.haomeiwen.com/subject/xonydctx.html