美文网首页LeetCode
273. Integer to English Words

273. Integer to English Words

作者: 小万叔叔 | 来源:发表于2017-01-20 12:34 被阅读2次
    /*
     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

        本文链接:https://www.haomeiwen.com/subject/xavwbttx.html