成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] The Number In Words

姘存按 / 1204人閱讀

Problem

Given a non-negative integer n, print the number in words.

Example

Given n = 125

Return one hundred twenty five

Solution
class Solution {
    private String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    private String[] tenToTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private String[] tens = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    
    public String convertWords(int num) {
        if (num == 0) return "zero";
        return helper(num);
    }
    
    private String helper(int num) {
        String result;
        if (num < 10) result = belowTen[num];
        else if (num < 20) result = tenToTwenty[num-10];
        else if (num < 100) result = tens[num/10] + " " + belowTen[num%10];
        else if (num < 1000) result = belowTen[num/100] + " Hundred " + helper(num%100);
        else if (num < 10000) result = belowTen[num/1000] + " Thousand " + helper(num%1000);
        else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num%1000);
        else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num%1000000);
        else result = helper(num/1000000000) + " Billion " + helper(num%1000000000);
        return result.trim().toLowerCase();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71520.html

相關(guān)文章

  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • [LeetCode/LintCode] Sentence Similarity

    Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...

    dreamtecher 評論0 收藏0
  • [LintCode] Longest Words

    Problem Given a dictionary, find all of the longest words in the dictionary. Example Given { dog, google, facebook, internationalization, blabla } the longest words are(is) [internationaliz...

    roadtogeek 評論0 收藏0
  • [LintCode] Implement Trie

    Problem Implement a trie with insert, search, and startsWith methods. Example insert(lintcode) search(code) // return false startsWith(lint) // return true startsWith(linterror) // return false insert...

    chengjianhua 評論0 收藏0
  • [LeetCode/LintCode] Word Ladder

    摘要:使用,利用其按層次操作的性質(zhì),可以得到最優(yōu)解。這樣可以保證這一層被完全遍歷。每次循環(huán)取出的元素存為新的字符串。一旦找到和相同的字符串,就返回轉(zhuǎn)換序列長度操作層數(shù),即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...

    張金寶 評論0 收藏0

發(fā)表評論

0條評論

姘存按

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<