[Swift]LeetCode848. 字母移位 | Shifting Letters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-drbmchpq-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

We have a string S of lowercase letters, and an integer array shifts.git

Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a'). github

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 Sx times.微信

Return the final string after all such shifts to S are applied.app

Example 1:ide

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:spa

  1. 1 <= S.length = shifts.length <= 20000
  2. 0 <= shifts[i] <= 10 ^ 9

有一個由小寫字母組成的字符串 S,和一個整數數組 shiftscode

咱們將字母表中的下一個字母稱爲原字母的 移位(因爲字母表是環繞的, 'z' 將會變成 'a')。htm

例如·,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. 1 <= S.length = shifts.length <= 20000
  2. 0 <= shifts[i] <= 10 ^ 9

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 }
相關文章
相關標籤/搜索