LeetCode 273. Integer to English Words

21 篇文章 0 订阅
12 篇文章 0 订阅

原题网址: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值