力扣 hot100 Day54
20. 有效的括号
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 s
,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 每个右括号都有一个对应的相同类型的左括号。
//自己写的
class Solution {
public:bool isValid(string s) {stack<char> kuohao;for (char c : s){if(!kuohao.empty()&&((kuohao.top()=='('&&c==')')||(kuohao.top()=='{'&&c=='}')||(kuohao.top()=='['&&c==']'))){kuohao.pop();}else{kuohao.push(c);}}return kuohao.empty();}
};
栈的基础题,水一天