#3782. [GESP202503 六级 C++] 第 14 题

[GESP202503 六级 C++] 第 14 题

以下代码用于检查字符串中的括号是否匹配,横线上应填写( )。

bool isBalanced(string s) {
    stack<char> st;
    for (char c : s) {
        if (c == '(' || c == '[' || c == '{') {
            st.push(c);
        } else {
            if (st.empty()) return false; // 无左括号匹配
            char top = st.top();
            st.pop();
            if ((c == ')' && top != '(') ||
                (c == ']' && top != '[') ||
                (c == '}' && top != '{')) {
                return false;
            }
        }
    }
    return ________________; //在此处填入代码
}

{{ select(1) }}

  • true
  • false
  • st.empty()
  • !st.empty()