在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ An encoded string
Now for some encoded string Example 1: Input: S = 10
Output:
Example 2: Input: S = 5
Output:
Example 3: Input: S = 1
Output:
Note:
给定一个编码字符串
现在,对于给定的编码字符串 示例 1: 输入: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"。 提示:
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 }
|
请发表评论