https://leetcode.com/problems/integer-to-english-words/

这题记得是《c 和指针》里的一道习题,三年前花了一晚上做过。现在花了大概40 分钟。

我的思路是三位三位的处理。三位与三位之间通过简单的用量级(Thousand, Million, Billion)拼接。特殊处理两位数的情况:小于20 和大于20 两种情况。

1)小于20 的时候是:'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten','Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'

2)大于20 的时候,就是十位数的量级加上个位数,十位数的量级是:'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety';个位数可以重用上面小于20 时候的情况。

举例:12345 按三位分割的话写成:12,345

先处理 345

百位和十位分开处理 3  45

判断45 这个十位数十大于20 还是小于20,在这里是大于20 的,所以采用方式2

十位量级Forty 加上 各位量级 Five 最后等于 Forty Five

然后处理百位,百位是3,简单的用Three 加上量级Hundred

最后生成 Three Hundred Forty Five

此时第一组三位数处理完毕:12,345

处理12

按照同样的方法,判断其小于20,直接在情况1 的表里查询查询到其是Twelve

此时整个12345 被分成两个三元组并处理完毕,第二个三元组简单的拼接上量词Thousand 以后与前一个三元组的结果拼接:

Twelve [Thousand] Three Hundred Forty Five

各种edge case 需要处理,一是对于0 的处理,脏一点直接判断孰否为0,直接返回Zero,还有在处理三元组的时候需要判断各个元组是否为0

/**
* @param {number} num
* @return {string}
*/
var a = [
['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten','Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'],
['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
];
var delim = [
'Thousand',
'Million',
'Billion'
]
var numberToWords = function(num) {
var d = 0;
ret = [];
if (num === 0) return 'Zero';
while (num > 0) {
var w = convertTriple(num % 1000);
if (w.length > 0) {
if (d > 0) {
ret.unshift(delim[d - 1]);
}
ret = w.concat(ret);
}
d++;
num = parseInt(num / 1000);
}
return ret.join(" ");
} function convertTriple(triple) {
var ret = [];
if (triple === 0) return ret;
//deal with tenth
var tuple = triple % 100;
if (tuple > 0) {
if (tuple < 20) {
ret.push(a[0][tuple - 1]);
} else {
var g = tuple % 10;
var tenth = Math.floor(tuple / 10);
if (tenth > 1) {
ret.push(a[1][tenth - 2]);
}
if (g > 0) {
ret.push(a[0][g - 1]);
}
}
} //deal with hundred
var hundred = Math.floor(triple / 100);
if (hundred > 0) {
ret.unshift('Hundred');
ret.unshift(a[0][hundred - 1]);
}
return ret;
}

Integer to English Words的更多相关文章

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

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

  2. 【LeetCode】273. Integer to English Words

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

  3. 【LeetCode】Integer to English Words 解题报告

    Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...

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

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

  5. LeetCode Integer to English Words

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

  6. 273. Integer to English Words

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

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

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

  8. Integer to English Words 解答

    Question Convert a non-negative integer to its english words representation. Given input is guarante ...

  9. [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words

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

  10. Integer to English words leetcode java

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

随机推荐

  1. vue-router2使用

    条件:紧接前面vue.js开发环境搭建 1.在cmd输入:npm install vue-router,回车,等待,安装对应版本router: 2.在入口文件用 import vueRouter fr ...

  2. Bubble Cup 8 finals F. Bulbo (575F)

    题意: 给定初始位置,查询n次区间,每次查询前可以花费移动距离的代价来移动, 查询时需要花费当前位置到区间内最近的点的距离,求最小代价. 1<=n<=5000,1<=所有位置< ...

  3. document.compatMode属性

    document.compatMode用来判断当前浏览器采用的渲染方式. 官方解释: BackCompat:标准兼容模式关闭.CSS1Compat:标准兼容模式开启. 当document.compat ...

  4. java环境变量以及jdk、jre、jvm

    一.jdk,jre,jvm的了解:jdk全称java development kit即java开发工具包,是整个java的核心,包含了java运行环境jre.java工具包和java的基础类库: jr ...

  5. 关于C#中的 static

    一:感受 做商业项目才能更深刻和彻底的探索到技术的原理与真实面貌.以前在学校里面的时候这些C Sharp的基本语法,数据结构,面向对象背的滚瓜乱熟,那真得是背的!无论是从概念从理论上面来谈还是写一个小 ...

  6. 无中间变量交换swap(a,b)

    #include <stdio.h> /* 加减法 整型.浮点型(损失精度) */ void swap1(int *a,int *b) { *a=*a+*b; *b=*a-*b; *a=* ...

  7. 深度学习笔记——PCA原理与数学推倒详解

    PCA目的:这里举个例子,如果假设我有m个点,{x(1),...,x(m)},那么我要将它们存在我的内存中,或者要对着m个点进行一次机器学习,但是这m个点的维度太大了,如果要进行机器学习的话参数太多, ...

  8. 通过js调用android原生方法

    有时候我们有这样一个需求,监听html中控件的一些事件.例如点击html中某个按钮,跳转到别的activity,复制某段文本. 首先是对webview的设置: myWebView = (WebView ...

  9. css3 自定义字体的使用方法

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...

  10. 【SqlServer】empty table and delete table and create table

    1.建表 1 IF object_id (N'表名', N'U') IS NULL CREATE TABLE 表名 ( 2 id INT IDENTITY (1, 1) PRIMARY KEY ,.. ...