zoukankan      html  css  js  c++  java
  • leetcode844

    class Solution {
    public:
        bool backspaceCompare(string S, string T) {
            stack<char> ST1;
            for (int i = 0; i < S.length(); i++)
            {
                char c = S[i];
                if (c == '#')
                {
                    if (!ST1.empty())
                    {
                        ST1.pop();
                    }
                }
                else
                {
                    ST1.push(c);
                }
            }
    
            stack<char> ST2;
            for (int i = 0; i < T.length(); i++)
            {
                char c = T[i];
                if (c == '#')
                {
                    if (!ST2.empty())
                    {
                        ST2.pop();
                    }
                }
                else
                {
                    ST2.push(c);
                }
            }
    
            if (ST1.size() != ST2.size())
            {
                return false;
            }
            else
            {
                for (int i = 0; i < ST1.size(); i++)
                {
                    char s = ST1.top();
                    ST1.pop();
    
                    char t = ST2.top();
                    ST2.pop();
    
                    if (s != t)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    };
  • 相关阅读:
    特殊集合
    推箱子
    集合
    数组

    循环语句 练习题
    穷举与迭代
    循环语句
    练习题
    switch case
  • 原文地址:https://www.cnblogs.com/asenyang/p/9733579.html
Copyright © 2011-2022 走看看