题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution { public: bool isValid(string s) { int len=s.length(); stack<int> a; if(s[0]=='}'||s[0]==']'||s[0]==')'||len%2==1) { return false; } a.push(s[0]); for(int i=1;i<len;++i) { if(s[i]=='['||s[i]=='{'||s[i]=='(') { a.push(s[i]); } else { if((s[i]==')'&&a.top()=='(')||(s[i]==']'&&a.top()=='[')||(s[i]=='}'&&a.top()=='{')) { a.pop(); } else { break; } } } return a.empty(); } };
今天第一题,希望有个好开端。