[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_01/id_145/LeetCode_20_145.cpp [Back]  [Original]

class Solution {
public:
    bool isValid(string s) {
        map m;
        m[')'] = '(';
        m[']'] = '[';
        m['}'] = '{';
        stack my_stack;
        for(int i; i < s.size(); ++i) {
            if(!m.count(s[i])) my_stack.push(s[i]);
            if(m.count(s[i]) && my_stack.empty()) return false;
            if(m.count(s[i]) && my_stack.top() != m[s[i]]) return false;
            if(m.count(s[i]) && my_stack.top() == m[s[i]]) my_stack.pop();
        }
        return my_stack.empty();
        
    }
};

Web Proxy Viewer  |  New URL  |  Original Page