题解 | #最大体重的牛#
最大体重的牛
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; };