Leetcode: Valid Parentheses
与 POJ 上那道括号匹配相比, 这道可谓简单
思路:
堆栈存储符号, 遇到匹配弹出
代码:
#include <iostream> #include <stack> using namespace std; class Solution { public: bool isValid(string s) { stack<char> record; for(int i = 0; i < s.size(); i ++) { if(record.empty()) { record.push(s[i]); }else{ if((s[i] == '(' && record.top()==')') || (s[i] == '[' && record.top()==']') || (s[i] == '{' && record.top() == '}')) record.pop(); else{ record.push(s[i]); } } } return record.empty(); } };