首页 > 其他 > 详细

LeetCode[stack]: Valid Parentheses

时间:2014-11-05 09:19:22      阅读:300      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!