在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ An array is monotonic if it is either monotone increasing or monotone decreasing. An array Return 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,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 提示:
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 }
|
请发表评论