Given a non-empty string containing an out-of-order English representation of digits 0-9
, output the digits in ascending order.
Note:
- Input contains only lowercase English letters.
- Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
- Input length is less than 50,000.
Example 1:
Input: "owoztneoer" Output: "012"
Example 2:
Input: "fviefuro" Output: "45"
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字
0-9
。按升序输出原始的数字。
注意:
- 输入只包含小写英文字母。
- 输入保证合法并可以转换为原始的数字,这意味着像 "abc" 或 "zerone" 的输入是不允许的。
- 输入字符串的长度小于 50,000。
示例 1:
输入: "owoztneoer" 输出: "012" (zeroonetwo)
示例 2:
输入: "fviefuro" 输出: "45" (fourfive)
172ms
1 class Solution { 2 func originalDigits(_ s: String) -> String { 3 var res:String = String() 4 var counts:[Int] = [Int](repeating:0,count:128) 5 var nums:[Int] = [Int](repeating:0,count:10) 6 for char in s.characters 7 { 8 counts[char.ascii] += 1 9 } 10 nums[0] = counts[122]//z 11 nums[2] = counts[119]//w 12 nums[4] = counts[117]//u 13 nums[6] = counts[120]//x 14 nums[8] = counts[103]//g 15 //o 16 nums[1] = counts[111] - nums[0] - nums[2] - nums[4] 17 //h 18 nums[3] = counts[104] - nums[8] 19 //f 20 nums[5] = counts[102] - nums[4] 21 //s 22 nums[7] = counts[115] - nums[6] 23 //i 24 nums[9] = counts[105] - nums[6] - nums[8] - nums[5] 25 for i in 0..<nums.count 26 { 27 for j in 0..<nums[i] 28 { 29 res += String(i) 30 } 31 } 32 return res 33 } 34 } 35 36 extension Character 37 { 38 //属性:ASCII整数值(定义小写为整数值) 39 var ascii: Int { 40 get { 41 let s = String(self).unicodeScalars 42 return Int(s[s.startIndex].value) 43 } 44 } 45 }
176ms
1 class Solution { 2 func originalDigits(_ s: String) -> String { 3 var count: [Int] = Array(repeating: 0, count: 10) 4 let s = Array(s) 5 for char in s { 6 if char == "z" { count[0] += 1 } 7 if char == "w" { count[2] += 1 } 8 if char == "x" { count[6] += 1 } 9 if char == "s" { count[7] += 1 } 10 if char == "g" { count[8] += 1 } 11 if char == "u" { count[4] += 1 } 12 if char == "f" { count[5] += 1 } 13 if char == "h" { count[3] += 1 } 14 if char == "i" { count[9] += 1 } 15 if char == "o" { count[1] += 1 } 16 } 17 count[7] -= count[6] 18 count[5] -= count[4] 19 count[3] -= count[8] 20 count[9] = count[9] - count[5] - count[6] - count[8] 21 count[1] = count[1] - count[0] - count[2] - count[4] 22 23 var res: [String] = [] 24 for i in 0 ... 9 { 25 for j in 0 ..< count[i] { 26 res.append("\(i)") 27 } 28 } 29 return res.joined() 30 } 31 }
504ms
1 class Solution { 2 func originalDigits(_ s: String) -> String { 3 var charDic = [Character:Int]() 4 var result = "" 5 for i in s{ 6 if(charDic[i] == nil){ 7 charDic[i] = 1 8 }else{ 9 charDic[i] = charDic[i]! + 1 10 } 11 } 12 if(charDic["z"] != nil){ 13 let cnt = charDic["z"]! 14 let str = [Character](repeating: "0", count: cnt) 15 result.append(String.init(str)) 16 charDic["z"] = charDic["z"]! - cnt 17 charDic["e"] = charDic["e"]! - cnt 18 charDic["r"] = charDic["r"]! - cnt 19 charDic["o"] = charDic["o"]! - cnt 20 } 21 if(charDic["w"] != nil){ 22 let cnt = charDic["w"]! 23 let str = [Character](repeating: "2", count: cnt) 24 result.append(String.init(str)) 25 charDic["t"] = charDic["t"]! - cnt 26 charDic["w"] = charDic["w"]! - cnt 27 charDic["o"] = charDic["o"]! - cnt 28 } 29 if(charDic["x"] != nil){ 30 let cnt = charDic["x"]! 31 let str = [Character](repeating: "6", count: cnt) 32 result.append(String.init(str)) 33 charDic["s"] = charDic["s"]! - cnt 34 charDic["i"] = charDic["i"]! - cnt 35 charDic["x"] = charDic["x"]! - cnt 36 } 37 if(charDic["g"] != nil){ 38 let cnt = charDic["g"]! 39 let str = [Character](repeating: "8", count: cnt) 40 result.append(String.init(str)) 41 charDic["e"] = charDic["e"]! - cnt 42 charDic["i"] = charDic["i"]! - cnt 43 charDic["g"] = charDic["g"]! - cnt 44 charDic["h"] = charDic["h"]! - cnt 45 charDic["t"] = charDic["t"]! - cnt 46 } 47 if(charDic["u"] != nil){ 48 let cnt = charDic["u"]! 49 let str = [Character](repeating: "4", count: cnt) 50 result.append(String.init(str)) 51 charDic["f"] = charDic["f"]! - cnt 52 charDic["o"] = charDic["o"]! - cnt 53 charDic["u"] = charDic["u"]! - cnt 54 charDic["r"] = charDic["r"]! - cnt 55 } 56 if(charDic["t"] != nil && charDic["t"]! != 0){ 57 let cnt = charDic["t"]! 58 let str = [Character](repeating: "3", count: cnt) 59 result.append(String.init(str)) 60 charDic["t"] = charDic["t"]! - cnt 61 charDic["h"] = charDic["h"]! - cnt 62 charDic["r"] = charDic["r"]! - cnt 63 charDic["e"] = charDic["e"]! - cnt 64 charDic["e"] = charDic["e"]! - cnt 65 } 66 if(charDic["o"] != nil && charDic["o"]! != 0){ 67 let cnt = charDic["o"]! 68 let str = [Character](repeating: "1", count: cnt) 69 result.append(String.init(str)) 70 charDic["o"] = charDic["o"]! - cnt 71 charDic["n"] = charDic["n"]! - cnt 72 charDic["e"] = charDic["e"]! - cnt 73 } 74 if(charDic["f"] != nil && charDic["f"]! != 0){ 75 let cnt = charDic["f"]! 76 let str = [Character](repeating: "5", count: cnt) 77 result.append(String.init(str)) 78 charDic["f"] = charDic["f"]! - cnt 79 charDic["i"] = charDic["i"]! - cnt 80 charDic["v"] = charDic["v"]! - cnt 81 charDic["e"] = charDic["e"]! - cnt 82 } 83 if(charDic["v"] != nil && charDic["v"]! != 0){ 84 let cnt = charDic["v"]! 85 let str = [Character](repeating: "7", count: cnt) 86 result.append(String.init(str)) 87 charDic["s"] = charDic["s"]! - cnt 88 charDic["e"] = charDic["e"]! - cnt 89 charDic["v"] = charDic["v"]! - cnt 90 charDic["e"] = charDic["e"]! - cnt 91 charDic["n"] = charDic["n"]! - cnt 92 } 93 if(charDic["e"] != nil && charDic["e"]! != 0){ 94 let cnt = charDic["e"]! 95 let str = [Character](repeating: "9", count: cnt) 96 result.append(String.init(str)) 97 charDic["n"] = charDic["n"]! - cnt 98 charDic["i"] = charDic["i"]! - cnt 99 charDic["n"] = charDic["n"]! - cnt 100 charDic["e"] = charDic["e"]! - cnt 101 } 102 return String.init(result.sorted()) 103 } 104 }
原文地址:https://www.cnblogs.com/strengthen/p/10333942.html
时间: 2024-10-15 21:46:17