[Swift]LeetCode880. 索引處的解碼字符串 | Decoded String at Index

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

An encoded string S is given.  To find and write the decodedstring to a tape, the encoded string is read one character at a time and the following steps are taken:git

  • If the character read is a letter, that letter is written onto the tape.
  • If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total.

Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string.github

Example 1:微信

Input: S = "leet2code3", K = 10 Output: "o" Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode". The 10th letter in the string is "o". 

Example 2:app

Input: S = "ha22", K = 5 Output: "h" Explanation: The decoded string is "hahahaha". The 5th letter is "h". 

Example 3:less

Input: S = "a2345678999999999999999", K = 1 Output: "a" Explanation: The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".

Note:編碼

  1. 2 <= S.length <= 100
  2. S will only contain lowercase letters and digits 2through 9.
  3. S starts with a letter.
  4. 1 <= K <= 10^9
  5. The decoded string is guaranteed to have less than 2^63 letters.

給定一個編碼字符串 S。爲了找出解碼字符串並將其寫入磁帶,從編碼字符串中每次讀取一個字符,並採起如下步驟:spa

  • 若是所讀的字符是字母,則將該字母寫在磁帶上。
  • 若是所讀的字符是數字(例如 d),則整個當前磁帶總共會被重複寫 d-1 次。

如今,對於給定的編碼字符串 S 和索引 K,查找並返回解碼字符串中的第 K 個字母。 code

示例 1:htm

輸入:S = "leet2code3", K = 10
輸出:"o"
解釋:
解碼後的字符串爲 "leetleetcodeleetleetcodeleetleetcode"。
字符串中的第 10 個字母是 "o"。

示例 2:

輸入:S = "ha22", K = 5
輸出:"h"
解釋:
解碼後的字符串爲 "hahahaha"。第 5 個字母是 "h"。

示例 3:

輸入:S = "a2345678999999999999999", K = 1
輸出:"a"
解釋:
解碼後的字符串爲 "a" 重複 8301530446056247680 次。第 1 個字母是 "a"。 

提示:

  1. 2 <= S.length <= 100
  2. S 只包含小寫字母與數字 2 到 9 。
  3. S 以字母開頭。
  4. 1 <= K <= 10^9
  5. 解碼後的字符串保證少於 2^63 個字母。

Runtime: 4 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func decodeAtIndex(_ S: String, _ K: Int) -> String {
 3         var K = K
 4         var N:Int = 0
 5         var arrS:[Character] = Array(S)
 6         var countS:Int = S.count < K ? S.count : K        
 7         for i in 0..<countS
 8         {
 9             let num:Int = arrS[i].ascii
10             N = num >= 48 && num <= 57 ? N * (num - 48) : N + 1
11         }
12         var i = countS - 1
13         while (true)
14         {
15             let num:Int = arrS[i].ascii
16             if num >= 48 && num <= 57
17             {
18                 N /= num - 48
19                 K %= N
20             }
21             else if K % N == 0
22             {
23                 return String(arrS[i])
24             }
25             else
26             {
27                 N -= 1
28             }
29             i -= 1
30         }
31         return String()
32     }
33 }
34 
35 //Character擴展
36 extension Character  
37 {  
38   //Character轉ASCII整數值(定義小寫爲整數值)
39    var ascii: Int {
40        get {
41            return Int(self.unicodeScalars.first?.value ?? 0)
42        }       
43     }
44 }

4ms

 1 class Solution {
 2     func decodeAtIndex(_ S: String, _ K: Int) -> String {
 3         var stack: [Character] = []
 4         
 5         var charCount: Int = 0 
 6         var movingIndex: Int = 0 
 7         var S = Array(S)
 8         
 9         while movingIndex < S.count && charCount < K {
10             let character = S[movingIndex]
11             if character.isDigit() {
12                 charCount *= character.toDigit() 
13             } else {
14                 charCount += 1
15             }
16             movingIndex += 1
17         }
18         var k = K 
19         while movingIndex > 0 {
20             movingIndex -= 1 
21             let character = S[movingIndex]
22             if character.isDigit() {
23                 charCount /= character.toDigit() 
24                 k %= charCount 
25             } else {
26                 if k == 0 || k == charCount {
27                     return String(character)
28                 }
29                 charCount -= 1 
30             }
31         }
32         
33         return "" 
34     }
35 }
36 
37 extension Character {
38     func isDigit() -> Bool {
39         return Int(String(self)) != nil 
40     }
41     
42     func toDigit() -> Int {
43         return Int(String(self))!
44     }
45 }

12ms

 1 class Solution {
 2     let digiSets = Set<Character>("123456789")
 3     func decodeAtIndex(_ S: String, _ K: Int) -> String {
 4         if K == 0 {
 5             if !digiSets.contains(S.last!) { return String(S.last!) }
 6             return decodeAtIndex(String(S.dropLast()), K)
 7         }
 8 
 9         var lastStr = [Character]()
10         var lastCount = 0, curCount = 0
11         var chars = Array(S)
12         for i in chars.indices {
13             if digiSets.contains(chars[i]) {
14                 curCount *= Int(String(chars[i]))!
15                 if curCount >= K { return decodeAtIndex(String(lastStr), K % lastCount)}
16             }
17             else {
18                 curCount += 1
19                 if curCount == K  { return String(chars[i]) }
20             }
21             lastStr.append(chars[i])
22             lastCount = curCount
23         }
24         return ""
25     }
26 }

20ms

 1 class Solution {
 2     func decodeAtIndex(_ S: String, _ K: Int) -> String {        
 3         func isNum(_ char: Character) -> Bool {
 4             if char == "2" || char == "3" || char == "4" || char == "5" || char == "6" || char == "7" || char == "8" || char == "9" {
 5                 return true
 6             }
 7             return false
 8         }
 9         var size = 0, k = K
10         for char in S {
11             if isNum(char) {
12                 let str = String(char)
13                 size *= (Int(str) ?? 1-1)
14             } else {
15                 size += 1
16             }
17         }
18         for item in S.reversed() {
19             k %= size
20             if k == 0  && !isNum(item) {
21                 return String(item)
22             }
23             if isNum(item) {
24                 let str = String(item)
25                 size /= (Int(str) ?? 1)
26             } else {
27                 size -= 1
28             }
29         }
30         return ""        
31     }
32 }
相關文章
相關標籤/搜索