题解 | #最大体重的牛#

最大体重的牛

https://www.nowcoder.com/practice/0333d46aec0b4711baebfeb4725cb4de

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param op string字符串vector 
     * @param vals int整型vector<vector<>> 
     * @return int整型vector
     */
    void pop()
    {
        st.pop();
    }
    int top()
    {
        auto a = st.top();
        return a.second;
    }
    int getMax()
    {
        int result = 0;
        vector<pair<int,int>> temp;
        while(!st.empty())
        {
            auto a = st.top();
            st.pop();
            result = max(result,a.second);
            temp.push_back(a);
        }
        reverse(temp.begin(),temp.end());
        for (auto it : temp)
        {
            st.push(it);
        }
        return result;
    }
    void push(int& a,int& b)
    {
        st.push(make_pair(a,b));
    }
    vector<int> max_weight_cow(vector<string>& op, vector<vector<int> >& vals) 
    {
        // write code here
        vector<int> result;
        for (int i = 0;i < op.size();++i)
        {
            if (op[i] == "MaxCowStack")
            {
                result.push_back(-1);
            }
            else if (op[i] == "push")
            {
                push(vals[i][0],vals[i][1]);
                result.push_back(-1);
            }
            else if (op[i] == "getMax")
            {
                result.push_back(getMax());
            }
            else if (op[i] == "pop")
            {
                pop();
                result.push_back(-1);
            }
            else if (op[i] == "top")
            {
                result.push_back(top());
            }
        }
        return result;
    }
private:
    stack<pair<int,int>> st;
};

全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务