LeetCode第 20 题:有效的括号(C++)

    技术2023-10-04  103

    leetcode链接

    primer里面栈的典型应用了。。

    class Solution { public: bool isValid(string s) { int n = s.size(); if(n == 0) return true; if(n % 2) return false;//长度为奇数 stack<char> stk; stk.push(s[0]); for(int i = 1; i < n; ++i){ if(!stk.empty() && (s[i] == ')' && stk.top() == '('|| s[i] == ']' && stk.top() == '['|| s[i] == '}' && stk.top() == '{')) stk.pop(); else stk.push(s[i]); } return stk.empty(); } };
    Processed: 0.052, SQL: 9