原生代码写,不用类库了,杀鸡不用牛刀
滑动窗口的最大值
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); }