码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode[stack]: Valid Parentheses

时间:2014-11-05 09:21:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:   c++   leetcode   stack   算法   

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.

典型的栈的应用,直接附上代码:

C++ code
bool isValid(string s) { stack<char> parentheses; for (auto c : s) { if (c == ‘(‘ || c == ‘{‘ || c == ‘[‘) parentheses.push(c); else { if (parentheses.empty()) return false; else if (c == ‘)‘ && parentheses.top() != ‘(‘) return false; else if (c == ‘]‘ && parentheses.top() != ‘[‘) return false; else if (c == ‘}‘ && parentheses.top() != ‘{‘) return false; parentheses.pop(); } } return parentheses.empty(); }

LeetCode[stack]: Valid Parentheses

标签:   c++   leetcode   stack   算法   

原文地址:http://blog.csdn.net/chfe007/article/details/40798741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!