LeetCode 273. Integer to English Words

来源:互联网 发布:电脑自动开关机软件 编辑:程序博客网 时间:2024/06/03 21:41

原题网址:https://leetcode.com/problems/integer-to-english-words/

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 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"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
思路:一千为单位,递归+分治策略。

public class Solution {    private String[] tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};    private String[] teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};    private String[] ones = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};    private String[] thousands = {"Thousand", "Million", "Billion", "Trillion"};    private StringBuilder sb = new StringBuilder();    private void insert(String word) {        if (sb.length() > 0) sb.insert(0, " ");        sb.insert(0, word);    }    private void number(int num, int thousand) {        if (num <= 0) return;        int m1000 = num % 1000;        if (m1000 > 0 && thousand > 0) insert(thousands[thousand-1]);        int m100 = num % 100;        int m10 = num % 10;        if (m100 > 0) {            if (m10 == 0) {                insert(tens[m100/10-1]);            } else if (11 <= m100 && m100 <= 19) {                insert(teens[m100-11]);            } else if (m100 < 11) {                insert(ones[m100-1]);            } else {                insert(ones[m10-1]);                insert(tens[m100/10-1]);            }        }        int h = num % 1000 / 100;        if (h > 0) {            insert("Hundred");            insert(ones[h-1]);        }                number(num/1000, thousand+1);    }    public String numberToWords(int num) {        if (num == 0) return "Zero";        number(num, 0);        return sb.toString();    }}

另一种实现方式:

public class Solution {    private String[] thousands = {"", "Thousand", "Million", "Billion"};    private String[] ones = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};    private String[] teens = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};    private String[] tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};    private String hundred(int num) {        String words = "";        int ten = num % 100;        if (ten > 0 && ten % 10 == 0) words = tens[ten / 10 - 1];        else if (11 <= ten && ten <= 19) words = teens[ten-11];        else if (0 < ten && ten < 10) words = ones[ten];        else if (ten >= 20) words = tens[ten / 10 - 1] + " " + ones[ten % 10];        num /= 100;        if (num > 0) words = ones[num] + " Hundred" + (words.length()==0? "" : " ") + words;        return words;    }    public String numberToWords(int num) {        if (num == 0) return "Zero";        String words = "";        int t = 0;        while (num > 0) {            int hundred = num % 1000;            if (hundred > 0) words = hundred(hundred) + (thousands[t].length()==0? "": " ") + thousands[t] + (words.length()==0? "": " ") + words;            num /= 1000;            t ++;        }        return words;    }}



0 0