1. 程式人生 > >Leetcode-20:Valid Parentheses

Leetcode-20:Valid Parentheses

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        map<char, char> parentheses = {{'(',')'}, {'{','}'}, {'[',']'}};
        for(char c: s){
            if(c=='(' || c=='{' || c=='['){
                stk.push(c);
            }else{
                if(stk.size()==0) return false;
                if(parentheses[stk.top()]==c)  stk.pop();
                else return false;
            }      
        }
        if(stk.size()==0) return true;
        else return false;
        
    }
};