Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53cb78e

Browse files
author
Shuo
authored
Add: 132 Pattern
1 parent f8bfb7d commit 53cb78e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎problems/132-pattern/132_pattern.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
package p_132_pattern
2+
3+
func find132pattern(nums []int) bool {
4+
ak, ajStack := -1<<31, make([]int, 0, len(nums))
5+
for i := len(nums) - 1; 0 <= i; i-- {
6+
if nums[i] < ak {
7+
return true
8+
}
9+
for len(ajStack) > 0 && ajStack[len(ajStack)-1] < nums[i] {
10+
ak = ajStack[len(ajStack)-1]
11+
ajStack = ajStack[:len(ajStack)-1]
12+
}
13+
ajStack = append(ajStack, nums[i])
14+
}
15+
return false
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
package p_132_pattern
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected bool
8+
}
9+
10+
func TestFind132pattern(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 2, 3, 4},
14+
expected: false,
15+
},
16+
{
17+
input: []int{3, 1, 4, 2},
18+
expected: true,
19+
},
20+
{
21+
input: []int{-1, 3, 2, 0},
22+
expected: true,
23+
},
24+
}
25+
for _, tc := range tests {
26+
output := find132pattern(tc.input)
27+
if output != tc.expected {
28+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /