★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10392606.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.
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.
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]:
- 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].
- 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:
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:
- Length of both ring and key will be in range 1 to 100.
- There are only lowercase letters in both strings and might be some duplcate characters in both strings.
- It's guaranteed that string key could always be spelled by rotating the string ring.
视频游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。
给定一个字符串 ring,表示刻在外环上的编码;给定另一个字符串 key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。
最初,ring 的第一个字符与12:00方向对齐。您需要顺时针或逆时针旋转 ring 以使 key 的一个字符在 12:00 方向对齐,然后按下中心按钮,以此逐个拼写完 key 中的所有字符。
旋转 ring 拼出 key 字符 key[i] 的阶段中:
- 您可以将 ring 顺时针或逆时针旋转一个位置,计为1步。旋转的最终目的是将字符串 ring 的一个字符与 12:00 方向对齐,并且这个字符必须等于字符 key[i] 。
- 如果字符 key[i] 已经对齐到12:00方向,您需要按下中心按钮进行拼写,这也将算作 1 步。按完之后,您可以开始拼写 key 的下一个字符(下一阶段), 直至完成所有拼写。
示例:
输入: ring = "godding", key = "gd" 输出: 4 解释: 对于 key 的第一个字符 'g',已经在正确的位置, 我们只需要1步来拼写这个字符。 对于 key 的第二个字符 'd',我们需要逆时针旋转 ring "godding" 2步使它变成 "ddinggo"。 当然, 我们还需要1步进行拼写。 因此最终的输出是 4。
提示:
- ring 和 key 的字符串长度取值范围均为 1 至 100;
- 两个字符串中都只有小写字符,并且均可能存在重复字符;
- 字符串 key 一定可以由字符串 ring 旋转拼出。
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 }
所有评论(0)