-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind132pattern.java
More file actions
58 lines (54 loc) · 1.86 KB
/
find132pattern.java
File metadata and controls
58 lines (54 loc) · 1.86 KB
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
import java.util.*;
/**
* Author : WindAsMe
* File : find132pattern.java
* Time : Create on 18-6-21
* Location : ../Home/JavaForLeeCode2/find132pattern.java
* Function : LeeCode No.456
*/
public class find132pattern {
private static boolean find132patternResult(int[] nums) {
int third = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
// Iteration from end to begin
for (int i = nums.length - 1; i >= 0; --i) {
if (nums[i] < third) {
return true;
} else while (!stack.empty() && nums[i] > stack.peek()) {
third = stack.peek();
stack.pop();
}
stack.push(nums[i]);
}
return false;
}
// Brute force: TL
// private static boolean find132patternResult(int[] nums) {
// if (nums.length <= 2) {
// return false;
// } else {
// int temp1 = nums[0];
// int index = 0;
// while (index <= nums.length - 2) {
// for (int i = index + 1; i < nums.length; i++) {
// if (nums[i] > temp1) {
// int temp2 = nums[i];
// for (int j = i + 1 ; j < nums.length ; j ++ ) {
// if (temp2 > nums[j] && nums[j] > temp1) {
// //System.out.println("temp1: " + temp1 + " temp2: " + temp2 + " num[j]: " + nums[j]);
// return true;
// }
// }
// }
// }
// index ++;
// temp1 = nums[index];
// }
// return false;
// }
// }
public static void main(String[] args) {
int[] nums = {1,2,3,2};
System.out.println(find132patternResult(nums));
}
}