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.

中文:给出一个非负整数,输出该数字在英语对应的单词,数字小于2^31-1

例如:下面是三个转换的例子

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

4、解题方法

其实一开始我把题目想复杂了。这道题不用考虑英语中隔位要写“And”的情况。如101,在英语中的念法为“One Hundred And One”,在这道题中,只需要输出“One Hundred One”。因此,要AC本题只需要将对应位置的数字和数位写对就可以了。Java代码如下:

/**
 * @功能说明:LeetCode 273 - Integer to English Words
 * @开发人员:Tsybius2014
 * @开发时间:2015年10月12日
 */
public class Solution {
    
    /**
     * 按英语读法输出阿拉伯数字对应单词
     * @param num 数字,范围:[0,2^31-1]
     * @return
     */
    public String numberToWords(int num) {
        
        if (num < 0) {
            return "";
        }

        //数字为0直接返回
        if (num == 0) {
            return "Zero";
        }

        //左起段落
        int segment1 = num / 1000000000;           //段落1:十亿位-千亿位
        int segment2 = num % 1000000000 / 1000000; //段落2:百万位-亿位
        int segment3 = num % 1000000 / 1000;       //段落3:千位-十万位
        int segment4 = num % 1000;                 //段落4:个位-百位

        String result = "";
        
        if (segment1 > 0) {
            result += numToWordsLessThan1000(segment1) + " " + "Billion";
        }
        if (segment2 > 0) {
            result += numToWordsLessThan1000(segment2) + " " + "Million";
        }
        if (segment3 > 0) {
            result += numToWordsLessThan1000(segment3) + " " + "Thousand";
        }
        if (segment4 > 0) {
            result += numToWordsLessThan1000(segment4);
        }

        return result.trim();
    }
    
    /**
     * 按英语读法输出阿拉伯数字对应单词
     * @param num 数字,范围:(0,1000)
     * @return
     */
    private String numToWordsLessThan1000(int num) {
        
        if (num == 0 || num >= 1000) {
            return "";
        }
        
        String result = "";
        if (num >= 100) {
            result += numToWordsBase(num / 100) + " " + "Hundred";
        }
        num = num % 100;
        if (num > 20) {
            result += numToWordsBase(num / 10 * 10);
            if (num % 10 != 0) { 
                result += numToWordsBase(num % 10);
            }
        } else if (num > 0) {
            result += numToWordsBase(num);
        }
        
        return result;
    }

    /**
     * 按英语读法输出阿拉伯数字对应单词
     * @param num 数字,范围:(0,20)∪{30,40,50,60,70,80,90}
     * @return
     */
    private String numToWordsBase(int num) {
        String result = " ";
        switch (num) {
        case 1: result += "One"; break;
        case 2: result += "Two"; break;
        case 3: result += "Three"; break;
        case 4: result += "Four"; break;
        case 5: result += "Five"; break;
        case 6: result += "Six"; break;
        case 7: result += "Seven"; break;
        case 8: result += "Eight"; break;
        case 9: result += "Nine"; break;
        case 10: result += "Ten"; break;
        case 11: result += "Eleven"; break;
        case 12: result += "Twelve"; break;
        case 13: result += "Thirteen"; break;
        case 14: result += "Fourteen"; break;
        case 15: result += "Fifteen"; break;
        case 16: result += "Sixteen"; break;
        case 17: result += "Seventeen"; break;
        case 18: result += "Eighteen"; break;
        case 19: result += "Nineteen"; break;
        case 20: result += "Twenty"; break;
        case 30: result += "Thirty"; break;
        case 40: result += "Forty"; break;
        case 50: result += "Fifty"; break;
        case 60: result += "Sixty"; break;
        case 70: result += "Seventy"; break;
        case 80: result += "Eighty"; break;
        case 90: result += "Ninety"; break;
        }
        return result;
    }
}

END

时间: 2024-10-24 09:03:55

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

[LeetCode] 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 -&g

[LeetCode]47. 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 -&g

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

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

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 面试题62. 圆圈中最后剩下的数字

我的LeetCode:https://leetcode-cn.com/u/ituring/ 我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Algorithmcii LeetCode 面试题62. 圆圈中最后剩下的数字 题目 0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 例如,0.1.2.3.4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or