★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-fimfkbfj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door.git
Given a string ring, which represents the code engraved on the outer ring and another string key, which represents the keyword needs to be spelled. You need to find the minimum number of steps in order to spell all the characters in the keyword.github
Initially, the first character of the ring is aligned at 12:00 direction. You need to spell all the characters in the string key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at 12:00 direction and then by pressing the center button.數組
At the stage of rotating the ring to spell the key character key[i]:微信
Example:app
Input: ring = "godding", key = "gd" Output: 4 Explanation: For the first key character 'g', since it is already in place, we just need 1 step to spell this character. For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo". Also, we need 1 more step for spelling. So the final output is 4.
Note:dom
視頻遊戲「輻射4」中,任務「通向自由」要求玩家到達名爲「Freedom Trail Ring」的金屬錶盤,並使用錶盤拼寫特定關鍵詞才能開門。ide
給定一個字符串 ring,表示刻在外環上的編碼;給定另外一個字符串 key,表示須要拼寫的關鍵詞。您須要算出可以拼寫關鍵詞中全部字符的最少步數。函數
最初,ring 的第一個字符與12:00方向對齊。您須要順時針或逆時針旋轉 ring 以使 key 的一個字符在 12:00 方向對齊,而後按下中心按鈕,以此逐個拼寫完 key 中的全部字符。ui
旋轉 ring 拼出 key 字符 key[i] 的階段中:
示例:
輸入: ring = "godding", key = "gd" 輸出: 4 解釋: 對於 key 的第一個字符 'g',已經在正確的位置, 咱們只須要1步來拼寫這個字符。 對於 key 的第二個字符 'd',咱們須要逆時針旋轉 ring "godding" 2步使它變成 "ddinggo"。 固然, 咱們還須要1步進行拼寫。 所以最終的輸出是 4。
提示:
1 class Solution { 2 func findRotateSteps(_ ring: String, _ key: String) -> Int { 3 var n:Int = ring.count 4 var m:Int = key.count 5 var v:[[Int]] = [[Int]](repeating:[Int](),count:26) 6 var memo:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:m),count:n) 7 for i in 0..<n 8 { 9 v[ring[i].ascii - 97].append(i) 10 } 11 return helper(ring, key, 0, 0, &v, &memo) 12 } 13 14 func helper(_ ring: String, _ key: String,_ x:Int,_ y:Int,_ v:inout [[Int]],_ memo:inout [[Int]]) -> Int 15 { 16 if y == key.count {return 0} 17 if memo[x][y] != 0 {return memo[x][y]} 18 var res:Int = Int.max 19 var n:Int = ring.count 20 for k in v[key[y].ascii - 97] 21 { 22 var diff:Int = abs(x - k) 23 var step:Int = min(diff, n - diff) 24 res = min(res, step + helper(ring, key, k, y + 1, &v, &memo)) 25 } 26 memo[x][y] = res + 1 27 return memo[x][y] 28 } 29 } 30 31 extension String { 32 //subscript函數能夠檢索數組中的值 33 //直接按照索引方式截取指定索引的字符 34 subscript (_ i: Int) -> Character { 35 //讀取字符 36 get {return self[index(startIndex, offsetBy: i)]} 37 } 38 } 39 40 extension Character 41 { 42 //屬性:ASCII整數值(定義小寫爲整數值) 43 var ascii: Int { 44 get { 45 let s = String(self).unicodeScalars 46 return Int(s[s.startIndex].value) 47 } 48 } 49 }