[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecut
Three stones are on a number line at positions? Each turn,let‘s say the stones are currently at positions? The game ends when you cannot make any more moves,ie. the stones are in consecutive positions. When the game ends,what is the minimum and maximum number of moves that you could have made?? Return the answer as an length 2 array:? Example 1: Input: a = 1,b = 2,c = 5 Output: [1,2] Explanation: Move stone from 5 to 4 then to 3,or we can move it directly to 3.
Example 2: Input: a = 4,b = 3,c = 2 Output: [0,0] Explanation: We cannot make any moves.
Note:
三枚石子放置在数轴上,位置分别为? 每一回合,我们假设这三枚石子当前分别位于位置? 当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。 要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案: 示例 1: 输入:a = 1,b = 2,c = 5 输出:[1,2] 解释:将石子从 5 移动到 4 再移动到 3,或者我们可以直接将石子移动到 3。 示例 2: 输入:a = 4,b = 3,c = 2 输出:[0,0] 解释:我们无法进行任何移动。 提示:
Runtime:?8 ms
Memory Usage:?19.1 MB
1 class Solution { 2 func numMovesStones(_ a: Int,_ b: Int,_ c: Int) -> [Int] { 3 var arr:[Int] = [a,b,c].sorted(by:<) 4 let a = arr[0] 5 let b = arr[1] 6 let c = arr[2] 7 var minNum:Int = 0 8 if b - a == 2 || c - b == 2 9 { 10 minNum += 1 11 } 12 else 13 { 14 if b - a > 2 15 { 16 minNum += 1 17 } 18 if c - b > 2 19 { 20 minNum += 1 21 } 22 } 23 return [minNum,c - a - 2] 24 } 25 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |