[抄题]:

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 Hundred Forty Five"

Example 3:

Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

个位数是0的时候不用说zero,直接说英文名就行了

所以<10数组的第一位是“”

[思维问题]:

不知道怎么把数字分开,/ k % k就行了,但是这道题的重点并不是把数分开,而是如何处理10、20、100内有英文名的特殊数字以及hundred thousand million billion

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

“英文名” + 每次都新建字符串的helper函数连接一下就行了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

可能出现空格字符串啊,所以都先赋值给result, 最后再统一string.trim一下

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

记住10、20、100内有英文名的特殊数字以及hundred thousand million billion 才有英文名

helper函数中每次都新建字符串,才能和一般字符串用+连接

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

public class Solution {
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public String numberToWords(int num) {
if (num == 0) return "Zero";
return helper(num);
} private String helper(int num) {
String result = new String();
if (num < 10) result = belowTen[num];
else if (num < 20) result = belowTwenty[num -10];
else if (num < 100) result = belowHundred[num/10] + " " + helper(num % 10);
else if (num < 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
else if (num < 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
else if (num < 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}

273. Integer to English Words数字转为单词的更多相关文章

  1. [LeetCode] 273. Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  2. leetcode-【hard】273. Integer to English Words

    题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...

  3. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  4. 【LeetCode】273. Integer to English Words

    Integer to English Words Convert a non-negative integer to its english words representation. Given i ...

  5. 273. Integer to English Words

    题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...

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

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. LeetCode 273. Integer to English Words

    原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...

  8. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  9. [LC] 273. Integer to English Words

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

随机推荐

  1. CentOS 添加常用 yum 源(转)

    CentOS 的官方源去掉了一些与版权有关的软件,因此想要安装这些软件或者手动下载安装,或者使用其他源. 下面我推荐常用的两个源, 这两个源基本可以满足一般服务器的使用需求. 首先, 添加源之前要确定 ...

  2. linux中控操作相关

    1.首先生成无密码登陆密钥 一般使用rsa 2.编写shell脚本 work_dir=$(pwd) 3.远程拷贝 work_dir=$(pwd) ..} do ¥{host_prefix}$i:$ e ...

  3. 使用JDK合成照片

    原图(工程所在目录7098849.jpg): 头像(工程所在目录20181023201750.jpg): 开始合成(执行如下main方法): public static void main(Strin ...

  4. 常见企业IT支撑【1、办公网络IP地址规划】

    规划思路如下,可灵活变化

  5. 【原】C++11并行计算 — 数组求和

    本文转载请注明出处 -- polobymulberry-博客园 0x00 - 前言 最近想优化ORB-SLAM2,准备使用并行计算来提高其中ORB特征提取的速度.之前对并行计算方面一窍不通.借此机会, ...

  6. webpack快速入门(三):资源管理

    上一章说了基本的webpack是用,包括命令行打包,npm脚本打包等基础的东西. 这篇说一下webpack的资源管理,包括(图片,字体,数据),首先调整一下项目结构成: webpack-demo |- ...

  7. HTML转义字符表

  8. Java复习——多线程与并发库

    开启一个线程 实现一个线程的方式有两种:继承Thread类.实现Runnable接口(也存在说三种的情况,第三种是使用线程并发库中的线程池创建一个线程).这两种方法都需要重写Run方法,具体的线程逻辑 ...

  9. jQuery笔记——UI

    jQuery UI 的官网网站为:http://jqueryui.com/,我们下载最新版本的即可,使用JQueryUI中的样式比我们使用原生的HTML要好看,还会有一些封装好的特效,JQueryUI ...

  10. php 无限分类

    <?php function genTree($items,$id='id',$pid='pid',$son = 'children'){ $tree = array(); $tmpMap = ...