[Swift]LeetCode1144. 递减元素使数组呈锯齿状 | Decrease Eleme
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an array? An array?
Return the minimum number of moves to transform the given array? Example 1: Input: nums = [1,2,3] Output: 2 Explanation: We can decrease 2 to 0 or 3 to 1. Example 2: Input: nums = [9,6,1,2] Output: 4? Constraints:
给你一个整数数组? 如果符合下列情况之一,则数组?
返回将数组? 示例 1: 输入:nums = [1,3] 输出:2 解释:我们可以把 2 递减到 0,或把 3 递减到 1。 示例 2: 输入:nums = [9,2] 输出:4? 提示:
Runtime:?8 ms
Memory Usage:?20.8 MB
1 class Solution { 2 func movesToMakeZigzag(_ nums: [Int]) -> Int { 3 var n:Int = nums.count 4 var j:Int = 0 5 var s:Int = 0 6 var t:Int = 0 7 for i in stride(from:0,to:n,by:2) 8 { 9 j = 0 10 if i != 0 11 { 12 j = max(j,nums[i]-nums[i-1]+1) 13 } 14 if i + 1 < n 15 { 16 j = max(j,nums[i]-nums[i+1]+1) 17 } 18 s += j 19 } 20 for i in stride(from:1,by:2) 21 { 22 j = 0 23 if i != 0 24 { 25 j = max(j,nums[i]-nums[i-1]+1) 26 } 27 if i + 1 < n 28 { 29 j = max(j,nums[i]-nums[i+1]+1) 30 } 31 t += j 32 } 33 return min(s,t) 34 } 35 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Oracle中Union与Union All的区别(适用多个数据库
- 知道这20个正则表达式,能让你少写1,000行代码
- 正则表达式 – [0-9a-fA-F]与[0-9a-f-A-F]相同吗
- 王立平--SQLite,SQLiteOpenHelper的简单应用
- ocp Oracle.1z0-053 711--108
- react-native – 如何使设备令牌以本机方式响应
- 仅使用字母数字字符进行Postgresql搜索
- cocos2d粒子系统工具Particle Designer
- cocos2d: fullPathForFilename: No file found a
- XML解析之javascripe,json,java