forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path456. 132 Pattern.go
49 lines (46 loc) · 1002 Bytes
/
456. 132 Pattern.go
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
package leetcode
import (
"fmt"
"math"
)
// 解法一 单调栈
func find132pattern(nums []int) bool {
if len(nums) < 3 {
return false
}
num3, stack := math.MinInt64, []int{}
for i := len(nums) - 1; i >= 0; i-- {
if nums[i] < num3 {
return true
}
for len(stack) != 0 && nums[i] > stack[len(stack)-1] {
num3 = stack[len(stack)-1]
stack = stack[:len(stack)-1]
}
stack = append(stack, nums[i])
fmt.Printf("stack = %v \n", stack)
}
return false
}
// 解法二 暴力解法,超时!
func find132pattern1(nums []int) bool {
if len(nums) < 3 {
return false
}
for j := 0; j < len(nums); j++ {
stack := []int{}
for i := j; i < len(nums); i++ {
if len(stack) == 0 || (len(stack) > 0 && nums[i] > nums[stack[len(stack)-1]]) {
stack = append(stack, i)
} else if nums[i] < nums[stack[len(stack)-1]] {
index := len(stack) - 1
for ; index >= 0; index-- {
if nums[stack[index]] < nums[i] {
return true
}
}
}
}
}
return false
}