题解 | #最大体重的牛#
最大体重的牛
https://www.nowcoder.com/practice/0333d46aec0b4711baebfeb4725cb4de
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param op string字符串vector * @param vals int整型vector<vector<>> * @return int整型vector */ vector<int> max_weight_cow(vector<string>& op, vector<vector<int>>& vals) { // write code here stack<int> st; vector<int> ret, v; for (int i = 0; i < op.size(); ++i) { if (op[i] == "MaxCowStack") { ret.push_back(-1); } else if(op[i] == "push") { st.push(vals[i][1]); v.push_back(vals[i][1]); ret.push_back(-1); } else if (op[i] == "getMax") { if (v.empty()) ret.push_back(-1); else { sort(v.begin(), v.end()); ret.push_back(v[v.size() - 1]); } } else if (op[i] == "pop") { int t = st.top(); st.pop(); for (auto& x : v) if (x == t) x = -1; ret.push_back(-1); } else if(op[i] == "top") { if (st.empty()) ret.push_back(-1); else ret.push_back(st.top()); } } return ret; } };