题解 | #最大体重的牛#

最大体重的牛

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

全部评论

相关推荐

在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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