LeetCode Valid Parentheses

链接: https://oj.leetcode.com/problems/valid-parentheses/


括号匹配。用栈

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


原文地址:https://www.cnblogs.com/frankM/p/4399422.html