[Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10241068.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
Example 1:
Input: 123 Output: "One Hundred Twenty Three"
Example 2:
Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。
示例 1:
输入: 123 输出: "One Hundred Twenty Three"
示例 2:
输入: 12345 输出: "Twelve Thousand Three Hundred Forty Five"
示例 3:
输入: 1234567 输出: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:
输入: 1234567891 输出: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
28ms
1 class Solution { 2 func numberToWords(_ num: Int) -> String { 3 var lessThan20 = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]; 4 var tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]; 5 var thousands = ["", "Thousand", "Million", "Billion"] 6 7 var result = "" 8 var i = 0 9 var num = num 10 11 if num == 0 { 12 return "Zero" 13 } 14 15 func helper(_ num: Int) -> String { 16 if num == 0 { 17 return "" 18 } else if num < 20 { 19 return lessThan20[num] + " " 20 } else if num < 100 { 21 return tens[num / 10] + " " + helper(num % 10) 22 } else { 23 return lessThan20[num / 100] + " Hundred " + helper(num % 100) 24 } 25 } 26 27 while num > 0 { 28 29 if num % 1000 != 0 { 30 result = helper(num % 1000) + thousands[i] + " " + result 31 } 32 num = num / 1000 33 34 i += 1 35 } 36 result = result.trimmingCharacters(in: CharacterSet.whitespaces) 37 return result 38 } 39 }
28ms
1 class Solution { 2 func numberToWords(_ num: Int) -> String { 3 4 if num == 0 { 5 return "Zero" 6 } 7 var num = num 8 var res = "" 9 let oneBillion = 1000000000 10 if num >= oneBillion { 11 res += suffixs(num/oneBillion) + " Billion" 12 num %= oneBillion 13 } 14 15 let oneMillion = 1000000 16 if num >= oneMillion { 17 res += suffixs(num/oneMillion) + " Million" 18 num %= oneMillion 19 } 20 21 let oneThousand = 1000 22 if num >= oneThousand { 23 res += suffixs(num/oneThousand) + " Thousand" 24 num %= oneThousand 25 } 26 27 if num > 0 { 28 res += suffixs(num) 29 } 30 31 return res.trimmingCharacters(in: .whitespaces) 32 } 33 34 func suffixs(_ n : Int) -> String { 35 let oneNum = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] 36 let tenNum = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] 37 let tyNum = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] 38 var n = n 39 var res = "" 40 if n > 99 { 41 res += " " + oneNum[n/100 - 1] + " Hundred" 42 } 43 44 n %= 100 45 46 if n > 19 { 47 res += " " + tyNum[n/10 - 2] 48 n %= 10 49 }else if n > 9 { 50 res += " " + tenNum[n - 10] 51 n = 0 52 } 53 if n > 0 { 54 res += " " + oneNum[n - 1] 55 } 56 57 return res 58 } 59 }
36ms
1 class Solution { 2 let zero2TwentyWords = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty"] 3 4 let tensWords = ["","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety","Hundred"] 5 let thousandsWords = ["" , "Thousand" , "Million" , "Billion"] 6 7 8 9 func numberToWords(_ num: Int) -> String { 10 if num == 0 { return "Zero"} 11 var temp = num 12 var res = "" 13 var i = 0 14 while temp > 0 { 15 if temp % 1000 != 0 { 16 res = helper(num: temp % 1000) + thousandsWords[i] + " " + res 17 } 18 temp = temp / 1000 19 i += 1 20 } 21 return res.trimmingCharacters(in: .whitespacesAndNewlines) 22 } 23 24 func helper(num:Int) -> String { 25 if num == 0 { 26 return "" 27 } else if num < 20 { 28 return zero2TwentyWords[num % 20] + " " 29 } else if num < 100 { 30 return tensWords[num / 10] + " " + helper(num: num % 10) 31 } else { 32 return zero2TwentyWords[num / 100] + " Hundred " + helper(num: num % 100) 33 } 34 } 35 36 }