题目: https://leetcode.com/problems/valid-parentheses/
概述: 符号匹配问题,问是否所有的开符号都对应一个闭符号。
思路: 用个栈即可
class Solution {
public:
bool isValid(string s) {
stack<char> paren;
int n = s.length();
for(int i = 0; i < n; i++)
{
if(s[i] == ')' && !paren.empty() && paren.top() == '(' ) paren.pop();
else if(s[i] == '}' && !paren.empty() && paren.top() == '{') paren.pop();
else if(s[i] == ']' && !paren.empty() && paren.top() == '[') paren.pop();
else paren.push(s[i]);
}
if(paren.empty())
return true;
else
return false;
}
};