LeetCode测验耗时2ms战胜98.93%的【整数转英文】算法实现

LeetCode测验耗时2ms战胜98.93%的【整数转英文】算法实现

解题思路:
  • 1、分析英语数值规律,分别将0-9、10-19、20-90定义成数组常量。
  • 2、将表示分位的单词也定义成数组常量。
  • 3、数值为0时,直接返回Zero
  • 3、其他情况,根据英语习惯,将整数分节,如:1234—>1,234;12345—>12,345,分节后,每节数值即在0~999之间,使用一维int数组存放即可。
  • 4、按节进行转换就简单许多了,同步再考虑分位的拼接:
    • 单节数值=0时无需转换。
    • 位于1~99之间的,着重处理:
      • 0-9的,对应上面定义的数组常量0~9;
      • 10-19的,对应上面的数组常量10~19;
      • 20-99的,将十位对应20-90的数组常量,个位对应0~9的数组常量。
    • 100-999的情况,先将百位对应0-9的数组常量,同时拼接百分位,再将个十位按上面1~99的方法进行转换拼接。
    • 最后每节处理完考虑一下分位即可。
代码实现:
  • Java:
package cn.yeoman.algorithm;

/**
 * Copyright © 2020年 All rights reserved.
 *
 * @author 倾杯 76164451@qq.com
 * Modification History:
 * Date           Author      Version     Description
 * ---------------------------------------------------------*
 * 2020/7/23     Yeoman       v1.0.0      创建
 */
public class Num2EnUtil {
    private static final String[] en0to9 = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    private static final String[] en10to19 = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private static final String[] en20to90 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private static final String[] enUnit = {" Hundred ", " Thousand ", " Million ", " Billion ", " Trillion "};

    public static String numberToWords(int num) {
        if (0 == num) {
            return en0to9[num];
        }
        int len = String.valueOf(num).length();
        int arrLen = len / 3;
        if (len % 3 > 0) {
            arrLen++;
        }
        int[] arr = new int[arrLen];
        int i = 0;
        while (num > 0) {
            arr[arrLen - 1 - i] = num % 1000;
            num /= 1000;
            i++;
        }
        StringBuilder sb = new StringBuilder();
        i = 0;
        for (; i < arrLen; i++) {
            num = arr[i];
            if (num == 0) {
                continue;
            } else if (num < 100) {
                ten(num, sb);
            } else {
                sb.append(en0to9[num / 100]).append(enUnit[0]);
                ten(num % 100, sb);
            }
            if (i < arrLen - 1) {
                if (sb.charAt(sb.length() - 1) == ' ') {
                    sb.setLength(sb.length() - 1);
                }
                sb.append(enUnit[arrLen - 1 - i]);
            }
        }
        return sb.toString().trim();
    }

    private static void ten(int n, StringBuilder sb) {
        if (n == 0) {
        } else if (n < 10) {
            sb.append(en0to9[n]);
        } else if (n < 20) {
            sb.append(en10to19[n % 10]);
        } else {
            sb.append(en20to90[n / 10]);
            if (n % 10 > 0) {
                sb.append(" ").append(en0to9[n % 10]);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(numberToWords(0));
        System.out.println(numberToWords(1));
        System.out.println(numberToWords(9));
        System.out.println(numberToWords(10));
        System.out.println(numberToWords(11));
        System.out.println(numberToWords(19));
        System.out.println(numberToWords(20));
        System.out.println(numberToWords(21));
        System.out.println(numberToWords(99));
        System.out.println(numberToWords(100));
        System.out.println(numberToWords(101));
        System.out.println(numberToWords(110));
        System.out.println(numberToWords(119));
        System.out.println(numberToWords(120));
        System.out.println(numberToWords(121));
        System.out.println(numberToWords(909));
        System.out.println(numberToWords(919));
        System.out.println(numberToWords(999));
        System.out.println(numberToWords(1000));
        System.out.println(numberToWords(1001));
        System.out.println(numberToWords(1010));
        System.out.println(numberToWords(1011));
        System.out.println(numberToWords(1021));
        System.out.println(numberToWords(1090));
        System.out.println(numberToWords(1099));
        System.out.println(numberToWords(10000));
        System.out.println(numberToWords(10001));
        System.out.println(numberToWords(10011));
        System.out.println(numberToWords(100119));
        System.out.println(numberToWords(123456));
        System.out.println(numberToWords(1234567));
        System.out.println(numberToWords(12345678));
        System.out.println(numberToWords(123456789));
        System.out.println(numberToWords(1234567890));
        System.out.println(numberToWords(1004067801));
    }
}
  • 执行结果:
Zero
One
Nine
Ten
Eleven
Nineteen
Twenty
Twenty One
Ninety Nine
One Hundred
One Hundred One
One Hundred Ten
One Hundred Nineteen
One Hundred Twenty
One Hundred Twenty One
Nine Hundred Nine
Nine Hundred Nineteen
Nine Hundred Ninety Nine
One Thousand
One Thousand One
One Thousand Ten
One Thousand Eleven
One Thousand Twenty One
One Thousand Ninety
One Thousand Ninety Nine
Ten Thousand
Ten Thousand One
Ten Thousand Eleven
One Hundred Thousand One Hundred Nineteen
One Hundred Twenty Three Thousand Four Hundred Fifty Six
One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
Twelve Million Three Hundred Forty Five Thousand Six Hundred Seventy Eight
One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine
One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety
One Billion Four Million Sixty Seven Thousand Eight Hundred One
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值