为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode896. 单调数列 | Monotonic Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10606658.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[i] >= A[j].

Return true if and only if the given array A is monotonic. 

Example 1:

Input: [1,2,2,3]
Output: true

Example 2:

Input: [6,5,4,4]
Output: true

Example 3:

Input: [1,3,2]
Output: false

Example 4:

Input: [1,2,4,5]
Output: true

Example 5:

Input: [1,1,1]
Output: true 

Note:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000

如果数组是单调递增或单调递减的,那么它是单调的

如果对于所有 i <= jA[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= jA[i]> = A[j],那么数组 A 是单调递减的。

当给定的数组 A 是单调数组时返回 true,否则返回 false。 

示例 1:

输入:[1,2,2,3]
输出:true

示例 2:

输入:[6,5,4,4]
输出:true

示例 3:

输入:[1,3,2]
输出:false

示例 4:

输入:[1,2,4,5]
输出:true

示例 5:

输入:[1,1,1]
输出:true 

提示:

  1. 1 <= A.length <= 50000
  2. -100000 <= A[i] <= 100000

500ms

 

 1 class Solution {
 2     enum Order: UInt8 {
 3         case increasing
 4         case decreasing
 5         case identical
 6         case nonMonotonic
 7     }
 8     
 9     func isMonotonic(_ A: [Int]) -> Bool {
10         guard A.count > 1 else {
11             return true
12         }
13 
14         var last = A[0]
15         var order: Order = .identical
16 
17         for idx in 1..<A.count {
18             let num = A[idx]
19             if num == last {
20                 continue
21             } else if (num < last && order == .increasing)
22             || (num > last && order == .decreasing) {
23                 order = .nonMonotonic
24                 break
25             } else if order == .identical {
26                 order = num < last ? .decreasing : .increasing
27             }
28             last = num
29         }
30         return order != .nonMonotonic
31     }
32 }

Runtime: 504 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var crement = true
 4         if A.last! < A.first! {
 5             crement = false
 6         }
 7         for i in 1..<A.count {
 8             if crement {
 9                 if A[i] < A[i-1] {
10                     return false
11                 }
12             } else {
13                 if A[i] > A[i-1] {
14                     return false
15                 }
16             }
17         }
18         return true
19     }
20 }

580ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         let count = A.count
 4         guard count > 2 else {
 5             return true
 6         }
 7         
 8         var flag: Bool?
 9         for i in 1..<count {
10             if A[i] > A[i - 1] {
11                 if let mark = flag {
12                     if !mark {
13                         return false
14                     }
15                 } else {
16                     flag = true
17                 }
18             } else if A[i] < A[i - 1] {
19                 if let mark = flag {
20                     if mark {
21                         return false
22                     }
23                 } else {
24                     flag = false
25                 }
26             }
27         }
28         return true
29     }
30 }

588ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var store: Int = 0
 4         for i in 0..<(A.count-1){
 5             let flag = compare(A[i] , A[i+1])
 6             
 7             if flag != 0{
 8                 if flag != store , store != 0{
 9                     return false
10                 }
11                 store = flag
12             }
13         }
14         return true
15     }
16     
17     func compare(_ lhs: Int, _ rhs: Int) -> Int{
18         if lhs == rhs {
19             return 0
20         }
21         else if lhs > rhs{
22             return 1
23         }
24         else{
25             return -1
26         }        
27     }
28 }

588ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3       if isIncreasing(A: A) || isDecreasing(A: A) {
 4           return true
 5       }
 6         return false
 7     }
 8     
 9     func isIncreasing(A: [Int]) -> Bool {
10         for i in 0..<A.count-1 {
11             if A[i] > A[i+1] {
12                 return false
13             }
14         }
15         return true
16     }
17     
18     func isDecreasing(A: [Int]) -> Bool {
19         for i in 0..<A.count-1 {
20             if A[i] < A[i+1] {
21                 return false
22             }
23         }
24         return true
25     }
26 }

590ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var isIncrease = true
 4         var isDecrease = true
 5         for i in 1..<A.count {
 6             if A[i-1] > A[i] {
 7                 isIncrease = false
 8                 break
 9             }
10         }
11         for i in 1..<A.count {
12             if A[i-1] < A[i] {
13                 isDecrease = false
14                 break
15             }
16         }
17         return isIncrease || isDecrease
18         
19     }
20 }

608ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var up = true
 4         var down = true
 5         
 6         for i in 1..<A.count {
 7             if !up || !down { }
 8             up = up && A[i - 1] <= A[i]
 9             down = down && A[i - 1] >= A[i]
10         }
11         
12         return up || down
13     }
14 }

916ms

 1 class Solution {
 2     func isMonotonic(_ A: [Int]) -> Bool {
 3         var flag = 0
 4         for i in 0..<A.count-1 {
 5             if A[i+1] - A[i] != 0 {
 6                 let c = A[i+1] - A[i]
 7                 if flag * c >= 0 {
 8                     flag = c
 9                 } else {
10                     return false
11                 }
12             }
13         }
14         return true
15     }
16 }

 

posted @ 2019-03-27 12:17  为敢技术  阅读(270)  评论(0编辑  收藏  举报