在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A subarray
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. Return the length of a maximum size turbulent subarray of A. Example 1: Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])
Example 2: Input: [4,8,12,16]
Output: 2
Example 3: Input: [100]
Output: 1
Note:
当
也就是说,如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是湍流子数组。 返回 示例 1: 输入:[9,4,2,10,7,8,8,1,9] 输出:5 解释:(A[1] > A[2] < A[3] > A[4] < A[5]) 示例 2: 输入:[4,8,12,16] 输出:2 示例 3: 输入:[100] 输出:1 提示:
676ms 1 class Solution { 2 func maxTurbulenceSize(_ A: [Int]) -> Int { 3 var n:Int = A.count 4 var dp:Int = 0 5 var ret:Int = 1 6 for i in 0..<(n - 1) 7 { 8 if A[i] < A[i+1] 9 { 10 if i > 0 && A[i-1] > A[i] 11 { 12 dp += 1 13 } 14 else 15 { 16 dp = 2 17 } 18 } 19 else if A[i] > A[i+1] 20 { 21 if i > 0 && A[i-1] < A[i] 22 { 23 dp += 1 24 } 25 else 26 { 27 dp = 2 28 } 29 } 30 else 31 { 32 dp = 1 33 } 34 ret = max(ret, dp) 35 } 36 return ret 37 } 38 }
|
请发表评论