标签:
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
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; } };
标签:
原文地址:http://blog.csdn.net/itismelzp/article/details/51588430