★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gynuqwrk-mc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Three stones are on a number line at positions a
, b
, and c
.git
Each turn, let's say the stones are currently at positions x, y, z
with x < y < z
. You pick up the stone at either position x
or position z
, and move that stone to an integer position k
, with x < k < z
and k != y
.github
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: answer = [minimum_moves, maximum_moves]
微信
Example 1:spa
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:code
Input: a = 4, b = 3, c = 2 Output: [0, 0] Explanation: We cannot make any moves.
Note:htm
1 <= a <= 100
1 <= b <= 100
1 <= c <= 100
a != b, b != c, c != a
三枚石子放置在數軸上,位置分別爲 a
,b
,c
。blog
每一回合,咱們假設這三枚石子當前分別位於位置 x, y, z
且 x < y < z
。從位置 x
或者是位置 z
拿起一枚石子,並將該石子移動到某一整數位置 k
處,其中 x < k < z
且 k != y
。遊戲
當你沒法進行任何移動時,即,這些石子的位置連續時,遊戲結束。
要使遊戲結束,你能夠執行的最小和最大移動次數分別是多少? 以長度爲 2 的數組形式返回答案:answer = [minimum_moves, maximum_moves]
示例 1:
輸入:a = 1, b = 2, c = 5 輸出:[1, 2] 解釋:將石子從 5 移動到 4 再移動到 3,或者咱們能夠直接將石子移動到 3。
示例 2:
輸入:a = 4, b = 3, c = 2 輸出:[0, 0] 解釋:咱們沒法進行任何移動。
提示:
1 <= a <= 100
1 <= b <= 100
1 <= c <= 100
a != b, b != c, c != a
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 }
1 class Solution { 2 func numMovesStones(_ a: Int, _ b: Int, _ c: Int) -> [Int] { 3 let arr = [a, b, c].sorted() 4 let a = arr[0] 5 let b = arr[1] 6 let c = arr[2] 7 8 if b - a == 1 { 9 if c - b == 1 { 10 return [0, 0] 11 } 12 return [1, c - b - 1] 13 } else if c - b == 1 { 14 return [1, b - a - 1] 15 } else { 16 if b - a == 2 || c - b == 2 { 17 return [1, c - a - 2] 18 } 19 return [2, c - a - 2] 20 } 21 } 22 }