1. 程式人生 > >[Swift]LeetCode423. 從英文中重建數字 | Reconstruct Original Digits from English

[Swift]LeetCode423. 從英文中重建數字 | Reconstruct Original Digits from English

order leet 原始的 fur () can tput turn nal

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

  1. Input contains only lowercase English letters.
  2. 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.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012" 

Example 2:

Input: "fviefuro"

Output: "45"

給定一個非空字符串,其中包含字母順序打亂的英文單詞表示的數字0-9。按升序輸出原始的數字。

註意:

  1. 輸入只包含小寫英文字母。
  2. 輸入保證合法並可以轉換為原始的數字,這意味著像 "abc" 或 "zerone" 的輸入是不允許的。
  3. 輸入字符串的長度小於 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 }

[Swift]LeetCode423. 從英文中重建數字 | Reconstruct Original Digits from English