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

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10333942.html

时间: 2024-10-15 21:46:17

[Swift]LeetCode423. 从英文中重建数字 | Reconstruct Original Digits from English的相关文章

[LeetCode] Reconstruct Original Digits from English 从英文中重建数字

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 origina

423. Reconstruct Original Digits from English (leetcode)

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 origina

[LeetCode]423. Reconstruct Original Digits from English

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 origina

LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路

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 origina

Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)

1 class Solution 2 { 3 public: 4 string originalDigits(string s) 5 { 6 vector<int> CharacterList(26,0); 7 for(auto c:s) 8 { 9 CharacterList[c-'a'] ++; 10 } 11 12 string result; 13 if(CharacterList['g'-'a']) 14 { 15 result.append(CharacterList['g'-'a

Leetcode: Reconstruct Original Digits from English

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 origina

Swift - 使用下划线(_)来分隔数值中的数字

为了增强较大数值的可读性,Swift语言增加了下划线(_)来分隔数值中的数字. 不管是整数,还是浮点数,都可以使用下划线来分隔数字. 1 2 3 4 //数值可读性 let value1 = 10_000_000_000 let value2 = 1_000_000.000_000_1 var value3:Int = 1_0_0_0_1

【转】正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度

匹配中文:[\u4e00-\u9fa5] 英文字母:[a-zA-Z] 数字:[0-9] 匹配中文,英文字母和数字及_: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$ 同时判断输入长度:[\u4e00-\u9fa5_a-zA-Z0-9_]{4,10} ^[\w\u4E00-\u9FA5\uF900-\uFA2D]*$ 1.一个正则表达式,只含有汉字.数字.字母.下划线不能以下划线开头和结尾:^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$  其中:^

python学习:判断字符串中字母数字空格的个数

'''输入一行字符,分别统计出包含英文字母.空格.数字和其它字符的个数. 统计出英文字母字符的个数 ''' # -*- coding: utf-8 -*- import re #正则表达式匹配 def isMathc(src,pat):     pattern = re.compile(pat)     result = re.match(pattern,src)     if result == None:         return 0     else:         return 1