原生代码写,不用类库了,杀鸡不用牛刀

滑动窗口的最大值

http://www.nowcoder.com/questionTerminal/1624bc35a45c42c0bc17d17fa0cba788

手动吧,不使用类库了,要是使用类库有点杀鸡用牛刀了

    public static ArrayList<Integer> maxInWindows(int [] num, int size)
    {
        ArrayList<Integer> result = new ArrayList<>();
        if(num == null || size > num.length || size == 0){
            return result;
        }
        // 定义双游标
        int i=0,j=i+size-1;
        int count = num.length - size;
        for(int n = 0;n<count+1;n++){
            getMax(num,result,i,j);
                // 游标联动
            i++;
            j++;
        }
        return result;
    }
    // 获取size中最大的数,算法随意挑选,我这里遍历
    private static void getMax(int [] num,ArrayList<Integer> result, int i, int j) {
        int h = i;
        int tmp = num[i];
        for(;h<j;h++){
            if(tmp<num[h+1]){
                tmp = num[h+1];
            }
        }
        result.add(tmp);
    }
全部评论
有没有超时啊?
点赞 回复 分享
发布于 2020-04-03 16:59

相关推荐

哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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