leetcode.844 比较含退格的字符串

这道题相对于前面几道简单难度题,还算比较简单,就是栈的运用,遇到‘#’就出栈,不是就进栈,然后在判断两个处理过后的字符串一样不。

class Solution {

public:

    bool backspaceCompare(string S, string T) {

        stack<char> stk;

        long len1 = S.size();

        long len2 = T.size();

        string s1, t1;

        for(int i = 0; i < len1; i++){

            if(S[i] != '#')

                stk.push(S[i]);

            else{

                if(stk.empty()){

                    continue;

                }

                else{

                    stk.pop();

                }

            }

        }

        while(!stk.empty()){

            s1 = stk.top() + s1;

            stk.pop();

        }

        for(int i = 0; i < len2; i++){

            if(T[i] != '#')

                stk.push(T[i]);

            else{

                if(stk.empty()){

                    continue;

                }

                else{

                    stk.pop();

                }

            }

        }

        while(!stk.empty()){

            t1 = stk.top() + t1;

            stk.pop();

        }

        return s1 == t1 ? true : false;

    }

};



猜你喜欢

转载自blog.csdn.net/torch_man/article/details/80554313