博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20. Valid Parentheses
阅读量:7114 次
发布时间:2019-06-28

本文共 985 字,大约阅读时间需要 3 分钟。

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 str){        stack
S; for(auto c: str){ switch(c){ case '(': case '[': case '{
': S.push(c); break; case ')': if(!S.size() || S.top() != '(') return false; else S.pop(); break; case ']': if(!S.size() || S.top() != '[') return false; else S.pop(); break; case '}': if(!S.size() || S.top() != '{
') return false; else S.pop(); break; default : return false; } } return !S.size(); }};

 

转载于:https://www.cnblogs.com/Pretty9/p/8593304.html

你可能感兴趣的文章