[LeetCode] Valid Palindrome

代码:

class Solution {
public:
    bool isValid(string s) {
        string left = "([{";
        string right = ")]}";
        stack<char> stk;

        for(auto c : s) {
            if(left.find(c) != string::npos)
                stk.push(c);
            else {
                if(stk.empty() || stk.top() != left[right.find(c)])
                    return false;
                else
                    stk.pop();
            }
        }

        return stk.empty();
    }
};

杂记:

1. string::npos

2. 其他解法一

不使用占,递归替换符号

原文地址:https://www.cnblogs.com/Azurewing/p/4310032.html