★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10591854.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
We have a string S
of lowercase letters, and an integer array shifts
.
Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z'
becomes 'a'
).
For example, shift('a') = 'b'
, shift('t') = 'u'
, and shift('z') = 'a'
.
Now for each shifts[i] = x
, we want to shift the first i+1
letters of S
, x
times.
Return the final string after all such shifts to S
are applied.
Example 1:
Input: S = "abc", shifts = [3,5,9] Output: "rpl" Explanation: We start with "abc". After shifting the first 1 letters of S by 3, we have "dbc". After shifting the first 2 letters of S by 5, we have "igc". After shifting the first 3 letters of S by 9, we have "rpl", the answer.
Note:
1 <= S.length = shifts.length <= 20000
0 <= shifts[i] <= 10 ^ 9
有一个由小写字母组成的字符串 S
,和一个整数数组 shifts
。
我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 'z'
将会变成 'a'
)。
例如·,shift('a') = 'b'
, shift('t') = 'u'
,, 以及 shift('z') = 'a'
。
对于每个 shifts[i] = x
, 我们会将 S
中的前 i+1
个字母移位 x
次。
返回将所有这些移位都应用到 S
后最终得到的字符串。
示例:
输入:S = "abc", shifts = [3,5,9] 输出:"rpl" 解释: 我们以 "abc" 开始。 将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。 再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。 最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。
提示:
1 <= S.length = shifts.length <= 20000
0 <= shifts[i] <= 10 ^ 9
1 class Solution { 2 func shiftingLetters(_ S: String, _ shifts: [Int]) -> String { 3 var arrS = Array(S) 4 var i:Int = shifts.count - 1 5 var m:Int = 0 6 while(i >= 0) 7 { 8 m += shifts[i] 9 arrS[i] = (((arrS[i].ascii - 97) + m) % 26 + 97).ASCII 10 i -= 1 11 m %= 26 12 } 13 return String(arrS) 14 } 15 } 16 17 //Character扩展 18 extension Character 19 { 20 //Character转ASCII整数值(定义小写为整数值) 21 var ascii: Int { 22 get { 23 return Int(self.unicodeScalars.first?.value ?? 0) 24 } 25 } 26 } 27 28 //Int扩展 29 extension Int 30 { 31 //Int转Character,ASCII值(定义大写为字符值) 32 var ASCII:Character 33 { 34 get {return Character(UnicodeScalar(self)!)} 35 } 36 }
652ms
1 class Solution { 2 func shiftingLetters(_ S: String, _ shifts: [Int]) -> String { 3 var number:UInt32 = 0 4 var i = 0; 5 var count = 0 ; 6 var string : String = "" 7 let startCount :UInt32 = 97 8 9 var newShifts :[Int] = [] //换算出一个新的数组 10 var allCount = 0 //算出总数 11 for val in shifts{ 12 let newval = val%26 13 newShifts .append(newval) 14 allCount += newval 15 } 16 for code in S.unicodeScalars { 17 number = code.value - startCount 18 19 number = (number + UInt32(allCount))%26 20 var ch:Character = Character(UnicodeScalar(number+startCount)!) 21 string.append(ch); 22 23 if(i < newShifts.count) 24 { 25 allCount -= newShifts[i] 26 } 27 else{ 28 allCount = 0 29 } 30 i += 1 31 } 32 return string 33 } 34 }
1568ms
1 class Solution { 2 func shiftingLetters(_ S: String, _ shifts: [Int]) -> String { 3 4 func move(s:String,steps:[Int]) -> String{ 5 let lastChar = Int("z".unicodeScalars.first!.value) 6 let firstChar = Int("a".unicodeScalars.first!.value) 7 let lenth = lastChar - firstChar + 1 8 let chars = zip(s,steps).map { (arg) -> Character in 9 print(arg.0,arg.0.unicodeScalars.first!.value) 10 let value = ((Int(arg.0.unicodeScalars.first!.value) - firstChar) + arg.1)%lenth + firstChar 11 12 return Character.init(Unicode.Scalar.init(value)!) 13 14 15 16 } 17 return String.init(chars) 18 } 19 20 var steps:[Int] = [] 21 22 var lastInt = 0 23 for i in stride(from: shifts.count-1, through: 0, by: -1) { 24 lastInt += shifts[i] 25 steps.append(lastInt) 26 } 27 return move(s:S ,steps:steps.reversed()) 28 } 29 }