[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Vali
发布时间:2020-12-14 05:09:46 所属栏目:百科 来源:网络整理
导读:Given an array? A ?of integers,return? true ?if and only if it is a? valid mountain array . Recall that A is a mountain array if and only if: A.length = 3 There exists some? i ?with? 0 i? A.length - 1 ?such that: A[0] A[1] ... A[i-1] A[i]
Given an array? Recall that A is a mountain array if and only if:
?Example 1: Input: [2,1]
Output: false
Example 2: Input: [3,5,5]
Output: false
Example 3: Input: [0,3,2,1]
Output: true
?Note:
给定一个整数数组? 让我们回顾一下,如果 A 满足下述条件,那么它是一个山脉数组:
示例 1: 输入:[2,1] 输出:false 示例 2: 输入:[3,5] 输出:false 示例 3: 输入:[0,1] 输出:true ?提示:
456ms 1 class Solution { 2 func validMountainArray(_ A: [Int]) -> Bool { 3 var n:Int = A.count 4 if n < 3 {return false} 5 var pre:Int = n - 1 6 for i in 0..<(n - 1) 7 { 8 if A[i] >= A[i + 1] 9 { 10 pre = i 11 break 12 } 13 } 14 if pre == 0 || pre == n-1 {return false} 15 for i in pre..<(n - 1) 16 { 17 if A[i] <= A[i + 1] {return false} 18 } 19 return true 20 } 21 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |