小编Raj*_*Sri的帖子

C++ 中一个简单堆栈问题的意外答案

class Solution {
public:
bool isValid(string s) {
    map<char , char> m;
    m[')'] = '(';
    m['}'] = '{';
    m[']'] = '[';
    
    stack<char> st;
    
    if(s[0] != '(' || s[0] != '{' || s[0] != '[')
        return "false";
    
    for(int i = 0; i<s.length(); i++)
    {
        if(s[i] == '(' || s[i]== '{' || s[i]== '[')
        {
            st.push(s[i]);
        }
        else if(st.top() == m[s[i]])
        {
            st.pop();
        }
        else if(st.top() != m[s[i]])
        {
            return "false";
        }

    }
    
    if(st.empty())
        return "true";
    else
        return "false";
}
};
Run Code Online (Sandbox Code Playgroud)

对于诸如“(]”之类的基本示例,代码失败。我不明白这是怎么可能的。 …

c++ stack dictionary

0
推荐指数
1
解决办法
58
查看次数

标签 统计

c++ ×1

dictionary ×1

stack ×1