重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

leetCode20.ValidParentheses字符串

20. Valid Parentheses

成都创新互联专业为企业提供芷江网站建设、芷江做网站、芷江网站设计、芷江网站制作等企业网站建设、网页设计与制作、芷江企业网站模板建站服务,10年芷江做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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) {
        if(s.size() == 0 || s.size() % 2 == 1)
            return false;
        map brackets;
    	brackets.insert(pair('}', '{'));
    	brackets.insert(pair(']', '['));
    	brackets.insert(pair(')', '('));
    	stack mystack;
    	for(int i = 0 ;i < s.size();i++)
    	{
    	    if(s[i] == '{' || s[i] == '[' || s[i] == '(' )
    	    {
    	        mystack.push(s[i]);
    	    }
    	    else
    	    {
    	        if( mystack.size() < 1)
    	            return false;
    	        if(brackets[s[i]] == mystack.top())
    	            mystack.pop();
    	        else
    	            return false;
    	    }
    	}
    	if(mystack.size() == 0)
    	    return true;
    	else 
    	    return false;
    }
};

2016-08-11 00:30:15


文章题目:leetCode20.ValidParentheses字符串
文章链接:http://cqcxhl.com/article/psgoci.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP