• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode273.整数转换英文表示|IntegertoEnglishWords

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10241068.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift中编写单例的正确方式发布时间:2022-07-13
下一篇:
iOSUIAlertController中加入倒计时,输入框,Swift讲解发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap