LeetCode 20 - Valid Parentheses

来源:互联网 发布:php下载文件无法打开 编辑:程序博客网 时间:2024/05/10 04:21

Valid Parentheses

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

My Code

class Solution {public:    bool isValid(string s) {        stack<char> parentheses;        for (int i = 0; i < s.size(); i++)        {            char c = s[i];            if (c == '(' || c == '[' || c == '{')                parentheses.push(c);            else            {                if (parentheses.empty())                    return false;                char cc = parentheses.top();                if (c == ')' && cc == '(' || c == ']' && cc == '[' || c == '}' && cc == '{')                    parentheses.pop();                else                    return false;            }        }        if (parentheses.empty())            return true;        else            return false;    }};
Runtime: 0 ms

0 0