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

leetcode 20 Valid Parentheses

时间:2017-02-10 23:32:51      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:top   pre   push   pop   ring   ++   amp   tac   stack   

class Solution {
public:
    bool isValid(string s) {
        stack<char> parentheses;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == ‘(‘ || s[i] == ‘[‘ || s[i] == ‘{‘) parentheses.push(s[i]);
            else {
                if (parentheses.empty()) return false;
                if (s[i] == ‘)‘ && parentheses.top() != ‘(‘) return false;
                if (s[i] == ‘]‘ && parentheses.top() != ‘[‘) return false;
                if (s[i] == ‘}‘ && parentheses.top() != ‘{‘) return false;
                parentheses.pop();
            }
        }
        return parentheses.empty();
    }
}; 

leetcode 20 Valid Parentheses

标签:top   pre   push   pop   ring   ++   amp   tac   stack   

原文地址:http://www.cnblogs.com/wangkun1993/p/6388142.html

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