LeetCode 273 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"

题目链接:https://leetcode.com/problems/integer-to-english-words/

题目分析:以Thousand,Million,Billion为分割点,三位三位的去处理,注意0的特殊情况

public class Solution {

    final String[] units = {"Zero", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
    final String[] tens1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
    final String[] tensn = {"", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
    final String[] base = {"Hundred ", "Thousand ", "Million ", "Billion "};
    final int Billion = 1000000000;
    final int Million = 1000000;
    final int Thousand = 1000;
    final int Hundred = 100;
    final int Ten = 10;

    //处理十位数
    public String TNumToWords(int num) {
        StringBuffer ans = new StringBuffer("");
        if(num / Ten == 1) {
            ans.append(tens1[num - Ten]);
        }
        else {
            ans.append(tensn[num / Ten]);
            if(num % Ten != 0) {
                ans.append(units[num % Ten]);
            }
        }
        return ans.toString();
    }

    //处理百位数
    public String HNumToWords(int num) {
        StringBuffer ans = new StringBuffer("");
        int H = num / Hundred;
        if(H < Ten) {
            ans.append(units[H]);
            ans.append(base[0]);
        }
        else if(H < Hundred) {
            ans.append(TNumToWords(H));
            ans.append(base[0]);
        }
        ans.append(TNumToWords(num % Hundred));
        return ans.toString();
    }

    //判断是个位十位还是百位数
    public String getType(int num) {
        StringBuffer ans = new StringBuffer("");
        if(num < Ten) {
            ans.append(units[num]);
        }
        else if(num < Hundred) {
            ans.append(TNumToWords(num));
        }
        else {
            ans.append(HNumToWords(num));
        }
        return ans.toString();
    }

    public String numberToWords(int num) {
        StringBuffer ans = new StringBuffer("");
        if(num == 0) {
            ans.append(units[0]);
            return ans.toString();
        }
        if(num / Billion != 0) {
            ans.append(getType(num / Billion));
            ans.append(base[3]);
            num %= Billion;
        }
        if(num / Million != 0) {
            ans.append(getType(num / Million));
            ans.append(base[2]);
            num %= Million;
        }
        if(num / Thousand != 0) {
            ans.append(getType(num / Thousand));
            ans.append(base[1]);
            num %= Thousand;
        }
        if(num != 0) {
            ans.append(getType(num));
        }
        ans.deleteCharAt(ans.length() - 1);
        return ans.toString();
    }
}
时间: 2024-10-12 02:30:33

LeetCode 273 Integer to English Words的相关文章

leetCode 273. Integer to English Words 字符串 | Hard

273. 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 Hundre

[LeetCode 273] Integer to English Word

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 -&g

[leetcode]273. Integer to English Words 整数转英文单词

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123Output: "One Hundred Twenty Three"Example 2: Input: 12345Output: "Twelve Thousand Three Hundred Fort

LeetCode:Integer to English Words - 按英语读法输出数字对应单词

1.题目名称 Integer to English Words(按英语读法输出数字对应单词) 2.题目地址 https://leetcode.com/problems/integer-to-english-words/ 3.题目内容 英文:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 中文:给出一个非负整数

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

leetcode-【hard】273. Integer to English Words

题目: 273. 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 Hu

273. Integer to English Words

// 看看人家的转换int为String吧,真的很不错啊!主要是在1000以内的数自由转换的时候我没做好 private static final String[] lessThan20 = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",

【leetcode】273. Integer to English Words

题目如下: Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hun

[email&#160;protected] [273] Integer to English Words (String &amp; Math)

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 -> "Tw