‹ 上一题 下一题 ›

g6-04852. GESP-6级-2024-09-单选-第04题 未做

1000 ms 256 MB 测试点 ? GESPGESP-62024-09单选客观题6级·栈和队列栈队列

采用如下代码实现检查输入的字符串括号是否匹配,横线上应填入的代码为( )。

#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool is_valid(string s) {
    stack<char> st;
    char top;

    for (char& ch : s) {
        if (ch == '(' || ch == '{' || ch == '[') {
            st.push(ch);    // 左括号入栈
        }
        else
        {
            if (st.empty())
                return false;
            ____________ // 在此处填入代码
            if ((ch == ')' && top != '(') ||
                (ch == '}' && top != '{') ||
                (ch == ']' && top != '[')) {
                return false;
            }
        }
    }

    return st.empty();      // 栈为空则说明所有括号匹配成功
}
  • A. ```cpp
    top = st.top(); st.pop();
- **B.** ```cpp
st.pop(); top = st.top();
  • C. ```cpp
    st.pop(); top = st.front();
- **D.** ```cpp
top = st.front(); st.pop();
未选择
输入
输出

            
答案