1. 程式人生 > >[Swift]LeetCode456. 132模式 | 32 Pattern

[Swift]LeetCode456. 132模式 | 32 Pattern

pop top pty 個數 pan als ride 設計 解釋

Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.

Note: n will be less than 15,000.

Example 1:

Input: [1, 2, 3, 4]

Output: False

Explanation: There is no 132 pattern in the sequence. 

Example 2:

Input: [3, 1, 4, 2]

Output: True

Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. 

Example 3:

Input: [-1, 3, 2, 0]

Output: True

Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

給定一個整數序列:a1, a2, ..., an,一個132模式的子序列 ai, aj, ak 被定義為:當 i < j < k時,ai < ak < aj。設計一個算法,當給定有 n 個數字的序列時,驗證這個序列中是否含有132模式的子序列。

註意:n 的值小於15000。

示例1:

輸入: [1, 2, 3, 4]

輸出: False

解釋: 序列中不存在132模式的子序列。

示例 2:

輸入: [3, 1, 4, 2]

輸出: True

解釋: 序列中有 1 個132模式的子序列: [1, 4, 2].

示例 3:

輸入: [-1, 3, 2, 0]

輸出: True

解釋: 序列中有 3 個132模式的的子序列: [-1, 3, 2], [-1, 3, 0] 和 [-1, 2, 0].

164ms
 1 struct Stack<T> {
 2     var
items = [T]() 3 mutating func push(item: T) { 4 items.append(item) 5 } 6 mutating func pop() -> T { 7 return items.removeLast() 8 } 9 func top() -> T { 10 return items.last! 11 } 12 func empty() -> Bool { 13 return items.count == 0 14 } 15 } 16 17 class Solution { 18 func find132pattern(_ nums: [Int]) -> Bool { 19 var third = Int.min 20 var s = Stack<Int>() 21 for i in stride(from: nums.count - 1, through: 0, by: -1) { 22 if nums[i] < third { 23 return true 24 }else { 25 while !s.empty() && (nums[i] > s.top()) { 26 third = s.top() 27 _ = s.pop() 28 } 29 s.push(item: nums[i]) 30 } 31 } 32 return false 33 } 34 }

324ms

 1 class Solution {
 2     func find132pattern(_ nums: [Int]) -> Bool {
 3         var hanoi = Array<Int>.init();
 4         var medNum = Int.min
 5         for index in stride(from: nums.count-1, through: 0, by: -1) {
 6             if nums[index] < medNum {
 7                 return true
 8             } else {
 9                 while (hanoi.count > 0 && hanoi.last! < nums[index]) {
10                     medNum = hanoi.last!
11                     hanoi.removeLast()
12                 }
13             }
14             hanoi.append(nums[index])
15         }
16         return false
17     }
18 }

1348ms

 1 class Solution {
 2     func find132pattern(_ nums: [Int]) -> Bool {
 3         if nums.count < 3{ 
 4             return false 
 5         }
 6         var newNums = [nums[0]]
 7         for i in 1..<nums.count {
 8             if nums[i] != nums[i-1]{
 9                 newNums.append(nums[i]);
10             }
11         }
12         if newNums.count < 3{ 
13             return false 
14         }
15         var minArr = [newNums[0]]
16         var min = newNums[0]
17         for i in newNums{
18             if i < min{
19                 min = i
20             }
21             minArr.append(min)
22         }
23         for i in 1..<newNums.count-1{
24             let a = minArr[i]
25             let b = newNums[i]
26             if a >= b{ 
27                 continue 
28             }
29             for j in (i+1)..<newNums.count{
30                 let c = newNums[j]
31                 if c > a && c < b {
32                     return true
33                 }
34             }
35         }
36         return false
37     }
38 }

4388ms

 1 class Solution {
 2     func find132pattern(_ nums: [Int]) -> Bool {
 3         if nums.count < 3 {
 4             return false
 5         }
 6         var i = nums.count
 7         var min = nums[i - 1]
 8         while (i > 0) {
 9             min = nums[i - 1]
10             var j = nums.count
11             var max = min
12             while (j > i) {
13                 let t = nums[j - 1]
14                 if t > min {
15                     if max > min {
16                         if t > max {
17                             return true
18                         }else {
19                             max = t
20                         }
21                     }else {
22                         max = t
23                     }
24                 }
25                 j = j - 1
26             }
27             i = i - 1
28         }
29         return false
30     }
31 }

6656ms

 1 class Solution {
 2     func find132pattern(_ nums: [Int]) -> Bool {
 3         var n:Int = nums.count
 4         var i:Int = 0
 5         var j:Int = 0
 6         var k:Int = 0
 7         while (i < n) {
 8             while (i < n - 1 && nums[i] >= nums[i + 1])
 9             {
10                 i += 1
11             }
12             j = i + 1;
13             while (j < n - 1 && nums[j] <= nums[j + 1])
14             {
15                  j += 1
16             }
17             k = j + 1
18             while (k < n) {
19                 if nums[k] > nums[i] && nums[k] < nums[j]
20                 {
21                      return true
22                 }
23                 k += 1
24             }
25             i = j + 1
26         }
27         return false
28     }
29 }

[Swift]LeetCode456. 132模式 | 32 Pattern