/* * @lc app=leetcode.cn id=20 lang=cpp * * [20] 有效的括号 */ // @lc code=start class Solution { public: bool isValid(string s) { stack<char> cs; for (char ch : s) { if (ch == '(' || ch == '[' || ch == '{') { cs.push(ch); } else{ if(cs.empty()){ return false; } char cmp = cs.top(); cs.pop(); if(ch == ')' && cmp != '(' || ch == ']' && cmp != '[' || ch == '}' && cmp != '{' ){ return false; } } } return cs.empty(); } }; // @lc code=end
标签:ch,return,lc,有效,括号,&&,cs,cmp From: https://www.cnblogs.com/angdh/p/17977017