题解 | #最大体重的牛#
最大体重的牛
https://www.nowcoder.com/practice/0333d46aec0b4711baebfeb4725cb4de
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param op string字符串一维数组
* @param vals int整型二维数组
* @return int整型一维数组
*/
public int[] max_weight_cow (String[] op, int[][] vals) {
// write code here
Stack<int[]> opStack = null;
List<int[]> maxWeight = null;
int[] result = new int[op.length];
for(int i = 0;i < op.length;i++){
switch (op[i]){
case "MaxCowStack":
opStack = new Stack<>();
maxWeight = new ArrayList<>();
result[i] = -1;
break;
case "push":
opStack.add(vals[i]);
maxWeight.add(vals[i]);
result[i] = -1;
break;
case "top":
int[] top= opStack.peek();
result[i] = top[1];
break;
case "pop":
maxWeight.remove(opStack.pop());
result[i] = -1;
break;
case "getMax":
List<Integer> weight = new ArrayList<>();
for(int j = 0;j < maxWeight.size();j++){
weight.add(maxWeight.get(j)[1]);
}
weight.sort(Comparator.reverseOrder());
result[i] = weight.get(0);
}
}
return result;
}
}

查看10道真题和解析