[Swift]LeetCode482. 密钥格式化 | License Key Formatting
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9799232.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings such that each group contains?exactly?K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
Given a non-empty string S and a number K, format the string according to the rules described above.
Example 1:
Input: S = "5F3Z-2e-9-w", K = 4Output: "5F3Z-2E9W"Explanation: The string S has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed.?
Example 2:
Input: S = "2-5g-3-J", K = 2Output: "2-5G-3J"Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.?
Note:
給定一個密鑰字符串S,只包含字母,數字以及 '-'(破折號)。N 個 '-' 將字符串分成了 N+1 組。給定一個數字 K,重新格式化字符串,除了第一個分組以外,每個分組要包含 K 個字符,第一個分組至少要包含 1 個字符。兩個分組之間用 '-'(破折號)隔開,并且將所有的小寫字母轉換為大寫字母。
給定非空字符串 S 和數字 K,按照上面描述的規則進行格式化。
示例 1:
輸入:S = "5F3Z-2e-9-w", K = 4輸出:"5F3Z-2E9W"解釋:字符串 S 被分成了兩個部分,每部分 4 個字符;注意,兩個額外的破折號需要刪掉。示例 2:
輸入:S = "2-5g-3-J", K = 2輸出:"2-5G-3J"解釋:字符串 S 被分成了 3 個部分,按照前面的規則描述,第一部分的字符可以少于給定的數量,其余部分皆為 2 個字符。?
提示:
1 class Solution { 2 func licenseKeyFormatting(_ S: String, _ K: Int) -> String { 3 var res:[String] = [String]() 4 for index in S.indices.reversed() 5 { 6 if S[index] != "-" 7 { 8 if res.count % (K + 1) == K 9 { 10 res.append("-") 11 } 12 res.append(String(S[index])) 13 } 14 } 15 //字符數組轉字符串 16 var str:String = String(res.joined(separator: "").reversed()) 17 return str.uppercased() 18 } 19 }
132ms
1 class Solution { 2 func licenseKeyFormatting(_ S: String, _ K: Int) -> String { 3 var bufferS: String = "" 4 var result: String = "" 5 6 if(S.count == 0) { return "" } 7 8 for c in S { 9 if(String(c) != "-") { 10 bufferS += (String(c)).uppercased() 11 } 12 } 13 14 var i: Int = bufferS.count % K == 0 ? K : bufferS.count % K 15 for c in bufferS { 16 if(i == 0) { 17 result += "-" 18 i = K 19 } 20 if(i > 0) { 21 result += String(c) 22 i -= 1 23 } 24 } 25 26 return result 27 } 28 }220ms
1 class Solution { 2 func licenseKeyFormatting(_ S: String, _ K: Int) -> String { 3 let s = S.replacingOccurrences(of: "-", with: "").uppercased() 4 let chas = [Character](s) 5 6 var res = "" 7 res.append(String(chas[..<(chas.count%K)])) 8 9 for i in stride(from: chas.count % K, to: chas.count, by: K) { 10 if !res.isEmpty { 11 res.append("-") 12 } 13 res.append(String(chas[i..<(i+K)])) 14 } 15 16 return res 17 } 18 }368ms
1 class Solution { 2 func licenseKeyFormatting(_ S: String, _ K: Int) -> String { 3 var stringArray = S.split(separator: "-").joined(separator: "").map { String($0) } 4 5 var returnString: String = "" 6 7 while(!stringArray.isEmpty) { 8 let subArray: String = Array(stringArray.suffix(K)).reduce("", +).uppercased() 9 for _ in 0..<subArray.count { 10 stringArray.removeLast() 11 } 12 returnString = stringArray.isEmpty ? "\(subArray)" + returnString : "-\(subArray)" + returnString 13 14 } 15 16 return returnString 17 } 18 }1052ms
1 class Solution { 2 func licenseKeyFormatting(_ S: String, _ K: Int) -> String { 3 4 var n = 0 5 for c in S { 6 if c != "-" { 7 n += 1 8 } 9 } 10 11 var num_g = n / K 12 var first = K 13 if n % K > 0 { 14 first = n % K 15 num_g += 1 16 } 17 18 var res = "" 19 var temp = "" 20 var count_g = 0 21 22 for c in S { 23 if c != "-" { 24 temp += String(c).uppercased() 25 if (count_g == 0 && temp.count == first && count_g != num_g-1) || (count_g < num_g-1 && temp.count == K) { 26 res += temp + "-" 27 temp = "" 28 count_g += 1 29 } else if (count_g == num_g-1 && temp.count == K) || (count_g == 0 && temp.count == first) { 30 res += temp 31 temp = "" 32 count_g += 1 33 } 34 } 35 } 36 37 return res 38 } 39 }?
轉載于:https://www.cnblogs.com/strengthen/p/9799232.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode482. 密钥格式化 | License Key Formatting的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法笔记和上机实战训练指南(附下载)
- 下一篇: Ubuntu 开机自动挂载硬盘