-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que239.java
82 lines (58 loc) · 1.88 KB
/
Que239.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @author: hyl
* @date: 2019/08/01
**/
public class Que239 {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0 || k > nums.length){
return new int[0];
}
int[] result = new int[nums.length - k + 1];
int index = 0;
for (int i = 0; i < nums.length - k; i++) {
int max = nums[i];
for (int j = i+1; j < k+i; j++) {
if (max < nums[j]){
max= nums[j];
}
}
result[index++] = max;
}
return result;
}
public int[] maxSlidingWindow1(int[] nums, int k) {
if (nums == null || nums.length < 2){
return nums;
}
//保存当前窗口最大值的数组位置
LinkedList<Integer> queue = new LinkedList<Integer>();
int[] result = new int[nums.length-k+1];
for (int i = 0; i < nums.length; i++) {
//保存从大到小,如果后面的比前面大,就依次弹出
while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]){
queue.pollLast();
}
queue.addLast(i);
//如果当前队首是已移动窗口,则弹出
if (queue.peek() <= i-k){
queue.poll();
}
//当窗口长度为K时,保存结果
if (i+1 >= k){
result[i+1-k] = nums[queue.peek()];
}
}
return result;
}
public static void main(String[] args) {
int[] ints = new Que239().maxSlidingWindow(
new int[]{1,3,-1,-3,5,3,6,7}
,3);
System.out.println(ints);
}
}