Leetcode 20. Valid Parentheses

来源:互联网 发布:故宫软件 编辑:程序博客网 时间:2024/05/19 13:21

这里写链接内容
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.

class Solution {public:    bool isValid(string s) {        stack<char> am;        for (auto i : s) {            if (i == '(' || i == '{' || i == '[')                am.push(i);            else if (am.empty())                return false;            if (i == ')') {                if (am.top() == '(')                    am.pop();                else                    return false;            }            if (i == ']') {                if (am.top() == '[')                    am.pop();                else                    return false;            }            if (i == '}') {                if (am.top() == '{')                    am.pop();                else                    return false;            }           }        if (am.empty())            return true;        return false;    }};
原创粉丝点击