-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17 isZigZag .java
More file actions
38 lines (28 loc) · 920 Bytes
/
17 isZigZag .java
File metadata and controls
38 lines (28 loc) · 920 Bytes
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
//017 isZigZag
//given an array, for each triplet, return if it is zigzag or not, and return them as the an array. 1 is zigzag 0 is not.
public class zigzagArray {
public zigzagArray() {
}
public boolean[] solution(int[] array) {
int n = array.length;
int[] res = new int[n - 2];
for(int i = 2; i < n; ++i) {
int a = array[i - 2];
int b = array[i - 1];
int c = array[i];
if (b > a && b > c || a > b && b < c) {
res[i - 2] = 1;
}
}
return res;
}
public static void main(String[] args) {
int[] array = new int[]{1, 2, 1, 3, 1, 2, 3, 2, 3, 1};
zigzagArray za = new zigzagArray();
boolean[] res = za.solution(array);
for(int i = 0; i < res.length; ++i) {
System.out.print(res[i]);
System.out.print(" ");
}
}
}