★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-rhlewvhx-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.)git
Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse).github
When you get an instruction "A", your car does the following: position += speed, speed *= 2
.微信
When you get an instruction "R", your car does the following: if your speed is positive then speed = -1
, otherwise speed = 1
. (Your position stays the same.)app
For example, after commands "AAR", your car goes to positions 0->1->3->3, and your speed goes to 1->2->4->-1.ide
Now for some target position, say the lengthof the shortest sequence of instructions to get there.spa
Example 1: Input: target = 3 Output: 2 Explanation: The shortest instruction sequence is "AA". Your position goes from 0->1->3.
Example 2: Input: target = 6 Output: 5 Explanation: The shortest instruction sequence is "AAARA". Your position goes from 0->1->3->7->7->6.
Note:code
1 <= target <= 10000
.你的賽車起始停留在位置 0,速度爲 +1,正行駛在一個無限長的數軸上。(車也能夠向負數方向行駛。)htm
你的車會根據一系列由 A(加速)和 R(倒車)組成的指令進行自動駕駛 。blog
當車獲得指令 "A" 時, 將會作出如下操做: position += speed, speed *= 2
。
當車獲得指令 "R" 時, 將會作出如下操做:若是當前速度是正數,則將車速調整爲 speed = -1
;不然將車速調整爲 speed = 1
。 (當前所處位置不變。)
例如,當獲得一系列指令 "AAR" 後, 你的車將會走過位置 0->1->3->3,而且速度變化爲 1->2->4->-1。
如今給定一個目標位置,請給出可以到達目標位置的最短指令列表的長度。
示例 1: 輸入: target = 3 輸出: 2 解釋: 最短指令列表爲 "AA" 位置變化爲 0->1->3
示例 2: 輸入: target = 6 輸出: 5 解釋: 最短指令列表爲 "AAARA" 位置變化爲 0->1->3->7->7->6
說明:
1 <= target(目標位置) <= 10000
。1 class Solution { 2 func racecar(_ target: Int) -> Int { 3 var res:Int = 0 4 var q:[(Int,Int)] = [(0, 1)] 5 var visited:Set<String> = ["0,1"] 6 while(!q.isEmpty) 7 { 8 for i in stride(from:q.count,to:0,by:-1) 9 { 10 var ele = q.removeFirst() 11 var pos:Int = ele.0 12 var speed:Int = ele.1 13 if pos == target {return res} 14 var newPos:Int = pos + speed 15 var newSpeed:Int = speed * 2 16 var key:String = String(newPos) + "," + String(newSpeed) 17 if !visited.contains(key) && newPos > 0 && newPos < (target * 2) 18 { 19 visited.insert(key) 20 q.append((newPos, newSpeed)) 21 } 22 newPos = pos 23 newSpeed = (speed > 0) ? -1 : 1 24 key = String(newPos) + "," + String(newSpeed) 25 if !visited.contains(key) && newPos > 0 && newPos < (target * 2) 26 { 27 visited.insert(key) 28 q.append((newPos, newSpeed)) 29 } 30 } 31 res += 1 32 } 33 return -1 34 } 35 }
2312ms
1 import Foundation 2 class Solution { 3 4 func racecar(_ target: Int) -> Int { 5 var queue = Queue<(Int, Int)>() 6 queue.enqueue((0,1)) 7 var visited = Set<String>() 8 visited.insert("0_1") 9 var level = 0 10 while !queue.isEmpty { 11 for i in 0...queue.count - 1 { 12 let cur = queue.dequeue()! 13 if cur.0 == target { 14 return level 15 } 16 let next1 = (cur.0 + cur.1, cur.1 << 1) 17 let key1 = "\(next1.0)_\(next1.1)" 18 19 if !visited.contains(key1) && 0 < next1.0 && next1.0 < target << 1 { 20 queue.enqueue(next1) 21 visited.insert(key1) 22 } 23 24 let next2 = (cur.0, cur.1 > 0 ? -1 : 1) 25 let key2 = "\(next2.0)_\(next2.1)" 26 if !visited.contains(key2) && 0 < next2.0 && next2.0 < target << 1 { 27 queue.enqueue(next2) 28 visited.insert(key2) 29 } 30 } 31 level += 1 32 } 33 return -1 34 } 35 } 36 37 public struct Queue<T> { 38 fileprivate var array = [T]() 39 40 public var isEmpty: Bool { 41 return array.isEmpty 42 } 43 44 public var count: Int { 45 return array.count 46 } 47 48 public mutating func enqueue(_ element: T) { 49 array.append(element) 50 } 51 52 public mutating func dequeue() -> T? { 53 if isEmpty { 54 return nil 55 } else { 56 return array.removeFirst() 57 } 58 } 59 60 public var front: T? { 61 return array.first 62 } 63 64 }