首页 > 其他 > 详细

LeetCode:Valid Parentheses

时间:2016-06-05 12:37:41      阅读:216      评论:0      收藏:0      [点我收藏+]

Valid Parentheses


Total Accepted: 111710 Total Submissions: 375574 Difficulty: 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.

Subscribe to see which companies asked this question

Hide Tags
 Stack String














c++ code:

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


LeetCode:Valid Parentheses

原文:http://blog.csdn.net/itismelzp/article/details/51588430

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