/*
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.
For example,
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
*/
/*
Thinking:
2^31 - 1 = 2147483647
2,147,483,647
1. 按照量级:Billion Million Thousand
2. 每个分割:Zero One Two Three Four Five Six Seven Eight Night (个位)
Ten Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety (十位)
(十位如果是1,则需要匹配10~19,Ten,Eleven,twelve,thirteen,fourteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen)
个位+Hundred (百位)
Step:
1. 先把整个数按 3 位划分
2. 然后每个分割遇到 0 则不解析
3. 如果数值就是 0,返回 Zero
按字符串来处理,如果按整除来处理更快,每次除以1000则可以按3为进行分割了,
而且还可以过滤前面的0
*/
func convIntToEng(_ intNumber: Int) -> String {
guard intNumber > 0, intNumber <= 2147483647 else {
return "Zero"
}
var number = intNumber
var intArray: [Int] = []
while number > 0 {
//把余数放入到数组
let subInt = number % 1000
intArray.append(subInt)
number = number / 1000
}
//20~90
let tenStrings = ["Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
//100
let hundrendString = "Hundred"
//0 ~ 19
let tenthStrings = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten","Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
func appendStrWithSpace(_ input: inout String, _ append: String){
if (!append.isEmpty){
input.append(" ")
input.append(append)
}
}
func subString(_ subInt: Int) -> String {
var retString = ""
//百位
var value = subInt / 100
if (value > 0) {
appendStrWithSpace(&retString, tenthStrings[Int(value)])
appendStrWithSpace(&retString, hundrendString)
}
//十位
value = subInt % 100
if (value > 0) {
if (value >= 20) {
let v = value / 10
appendStrWithSpace(&retString, tenStrings[Int(v)])
}
else { //20一下单独处理
appendStrWithSpace(&retString, tenthStrings[Int(value)])
return retString
}
}
//个位
value = value % 10
if (value > 0) {
appendStrWithSpace(&retString, tenthStrings[Int(value)])
}
return retString
}
let sectionsStrings = ["", "Thousand", "Million", "Billion"]
var retString = ""
for i in stride(from: intArray.count-1, through: 0, by: -1) {
//Error: 每个section内部的头部本来就合并了空格,所以这里不需要再增加空格
let sub = subString(intArray[i])
if (!sub.isEmpty) {
retString.append(sub)
appendStrWithSpace(&retString, sectionsStrings[i])
}
}
retString = retString.trimmingCharacters(in: NSCharacterSet.whitespaces)
return retString
}
convIntToEng(12345)
273. Integer to English Words
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题 Convert a non-negative integer to its english words re...
- Convert a non-negative integer to its english words repre...
- 最近发现一个很好的网站叫做LeetCodeOJ,注册一个账号之后就可以在上面刷题,进一步加深对语言的熟悉。多造点轮...
- 题目 Convert a non-negative integer to its english words re...