20. Valid Parentheses

来源:互联网 发布:第章启航网络 编辑:程序博客网 时间:2024/06/05 07:53

题目链接:https://leetcode.com/problems/valid-parentheses/#/description

Description

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 Answer

class Solution {public:    bool isValid(string s) {        stack<char> paren;        for(auto c : s){            switch(c){                case '(':                case '{':                case '[': paren.push(c); break;                case ')': if(paren.empty() || paren.top() != '(') return false; else paren.pop(); break;                case '}': if(paren.empty() || paren.top() != '{') return false; else paren.pop(); break;                case ']': if(paren.empty() || paren.top() != '[') return false; else paren.pop(); break;                default: ;            }        }        return paren.empty();    }};

Submission Details

73 / 73 test cases passed.
Status: 

Accepted

Runtime: 3 ms
Submitted: 3 minutes ago


0 0