在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We have a string Call the shift of a letter, the next letter in the alphabet, (wrapping around so that For example, Now for each Return the final string after all such shifts to 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:
有一个由小写字母组成的字符串 我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 例如·, 对于每个 返回将所有这些移位都应用到 示例: 输入:S = "abc", shifts = [3,5,9] 输出:"rpl" 解释: 我们以 "abc" 开始。 将 S 中的第 1 个字母移位 3 次后,我们得到 "dbc"。 再将 S 中的前 2 个字母移位 5 次后,我们得到 "igc"。 最后将 S 中的这 3 个字母移位 9 次后,我们得到答案 "rpl"。 提示:
Runtime: 340 ms
Memory Usage: 21 MB
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 }
|
请发表评论