Leetcode 20. Valid Parentheses (Easy) (cpp)

来源:互联网 发布:大连育知同创科技 编辑:程序博客网 时间:2024/05/21 11:01

Leetcode 20. Valid Parentheses (Easy) (cpp)

Tag: Stack, String

Difficulty: Easy


/*20. Valid Parentheses (Easy)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> str;for (int i = 0; i < s.length(); i++) {if (s[i] == '(' || s[i] == '{' || s[i] == '[') {str.push(s[i]);}else {if (str.empty()) return false;if ((s[i] == ')' && str.top() != '(') || (s[i] == '}' && str.top() != '{') || (s[i] == ']' && str.top() != '[')) return false;str.pop();}}return str.empty();}};


0 0
原创粉丝点击