[LeetCode] 273. Integer to English Words

软件结
• 阅读 2053

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"

Recursion

思路

三位一算,比如1234567,先算567,得到Five Hundred Sixty Seven,再算234,得到Two Hundred Thirty Four Thousand,算出的结果后面加上thousand,再算1,加上billion

代码

private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
public String numberToWords(int num) {
    if(num == 0) return "Zero";
    String word = "";
    int i = 0;
    while(num > 0){
        if(num % 1000 != 0){
            word = helper(num % 1000) + THOUSANDS[i] + " " + word;
        }
        num /= 1000;
        i++;
    }
    return word.trim();
}

private String helper(int num){
    if(num == 0) return "";
    if(num < 20) return LESS_THAN_20[num] + " ";
    else if(num < 100){
        return TENS[num/10] + " " + helper(num % 10);
    }
    else{
        return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100);
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
11个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
3年前
LeetCode 1009. 十进制整数的反码
原文链接: LeetCode1009.十进制整数的反码(https://my.oschina.net/ahaoboy/blog/3118044)https://leetcodecn.com/problems/complementofbase10integer/(https://www.oschina.net/action/GoToL
Stella981 Stella981
3年前
Exceptionless
<divid"cnblogs\_post\_body"class"blogpostbodycnblogsmarkdown"<h1id"exceptionless.netcore开源日志框架"Exceptionless.NetCore开源日志框架</h1<blockquote<p作者:markjiang7m2<b
Stella981 Stella981
3年前
LocalDateTime计算时间差
<divclass"htmledit\_views"id"content\_views"<pLocalDateTime为java8的新特性之一<br</p<p<br</p<pLocalDateTime.now()获得当前时间<br</p<p</p<h5</h5<divstyle"marginleft
Stella981 Stella981
3年前
LeetCode 007 Reverse Integer
题目描述:ReverseIntegerReversedigitsofaninteger.Example1: x123,return321Example2: x123,return321Haveyouthoughtaboutthis?Herearesomegoodquestions
Stella981 Stella981
3年前
Postman 使用方法详细介绍
1,下载安装:https://www.getpostman.com/apps2,打开Postman,如图所示:!(https://oscimg.oschina.net/oscnet/00f434cd831f2f74fea6f6d7b86bc46a751.png)3,创建一个接口项目!(https://oscimg.oschina.
Stella981 Stella981
3年前
HTML5新增input标签属性
一.inputtype属性1<formaction""2邮箱<inputtype"email"name""id""<inputtype"submit"value"提交"<br/<br/3手机号码<inputtype"tel"name
Stella981 Stella981
3年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Stella981 Stella981
3年前
Hadoop 气数已尽!逃离复杂性,拥抱云计算
!(https://oscimg.oschina.net/oscnet/355facaec00d46ee851ad87cfdfa754a.gif)作者|MattAsay,译者|杨志昂来源:高效开发运维导读:虽然大数据依然如日中天,但该领域曾经的领头羊Cloudera、Hortonworks和MapR三家公司最近步履
美凌格栋栋酱 美凌格栋栋酱
4个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(