[Swift]LeetCode514. 自由之路 | Freedom Trail

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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]:微信

  1. You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. The final purpose of the rotation is to align one of the string ring's characters at the 12:00 direction, where this character must equal to the character key[i].
  2. If the character key[i] has been aligned at the 12:00 direction, you need to press the center button to spell, which also counts as 1 step. After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you've finished all the spelling.

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

  1. Length of both ring and key will be in range 1 to 100.
  2. There are only lowercase letters in both strings and might be some duplcate characters in both strings.
  3. It's guaranteed that string key could always be spelled by rotating the string ring.

視頻遊戲「輻射4」中,任務「通向自由」要求玩家到達名爲「Freedom Trail Ring」的金屬錶盤,並使用錶盤拼寫特定關鍵詞才能開門。ide

給定一個字符串 ring,表示刻在外環上的編碼;給定另外一個字符串 key,表示須要拼寫的關鍵詞。您須要算出可以拼寫關鍵詞中全部字符的最少步數。函數

最初,ring 的第一個字符與12:00方向對齊。您須要順時針或逆時針旋轉 ring 以使 key 的一個字符在 12:00 方向對齊,而後按下中心按鈕,以此逐個拼寫完 key 中的全部字符。ui

旋轉 ring 拼出 key 字符 key[i] 的階段中:

  1. 您能夠將 ring 順時針或逆時針旋轉一個位置,計爲1步。旋轉的最終目的是將字符串 ring 的一個字符與 12:00 方向對齊,而且這個字符必須等於字符 key[i] 。
  2. 若是字符 key[i] 已經對齊到12:00方向,您須要按下中心按鈕進行拼寫,這也將算做 1 步。按完以後,您能夠開始拼寫 key 的下一個字符(下一階段), 直至完成全部拼寫。

示例:

 

 

輸入: ring = "godding", key = "gd"
輸出: 4
解釋:
 對於 key 的第一個字符 'g',已經在正確的位置, 咱們只須要1步來拼寫這個字符。 
 對於 key 的第二個字符 'd',咱們須要逆時針旋轉 ring "godding" 2步使它變成 "ddinggo"。
 固然, 咱們還須要1步進行拼寫。
 所以最終的輸出是 4。

提示:

  1. ring 和 key 的字符串長度取值範圍均爲 1 至 100;
  2. 兩個字符串中都只有小寫字符,而且都可能存在重複字符;
  3. 字符串 key 必定能夠由字符串 ring 旋轉拼出。

Runtime: 224 ms
Memory Usage: 19.7 MB
 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 }
相關文章
相關標籤/搜索