题解 | #最大体重的牛#

最大体重的牛

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;
};

全部评论

相关推荐

在看数据的傻狍子很忙碌:学生思维好重,而心很急,自己想想真的能直接做有难度的东西吗?任何错误都是需要人担责的,你实习生可以跑路,你的同事领导呢
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务