Integer to English Words
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的更多相关文章
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 【LeetCode】Integer to English Words 解题报告
Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- LeetCode Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to it ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- Integer to English Words 解答
Question Convert a non-negative integer to its english words representation. Given input is guarante ...
- [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- Integer to English words leetcode java
问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
随机推荐
- Zabbix自定义监控8080端口的连接数
Zabbix自定义监控8080端口的连接数 一 zabbix自定义监控实现思路 实际上我们要想使用zabbix来监控一些服务的原理很简单,步骤分别是:1.写一个脚本用于获取待监控服务的一些状态信息2. ...
- 【10-26】java调试技术学习笔记
调试工具 jdk自带的工具 jmap jconsole VisualVM jmap jmap -histo:live pid 列出该进程的所有活动实例统计信息 jmap -dump:live,file ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- Redhat6.4下安装Oracle10g
Oracle10g_Redhat6.4 安装指南 文档说明 本文借鉴<Redhat_Linux_6.4下Oracle_10g安装配置手册><Redhat 6.4 安装 Oracle1 ...
- FFT NNT
算算劳资已经多久没学新算法了,又要重新开始学辣.直接扔板子,跑...话说FFT算法导论里讲的真不错,去看下就懂了. //FFT#include <cstdio> #include < ...
- 数据库mysql 基本命令
.....= =.... 进入mysql: mysql -uroot ; 创建一个数据库: create database [数据库名字]; (注意最后的分号不能漏) 删除一个数据库:drop dat ...
- Thrift的TJsonProtocol协议分析
Thrift协议实现目前有二进制协议(TBinaryProtocol),紧凑型二进制协议(TCompactProtocol)和Json协议(TJsonProtocol). 前面的两篇文字从编码和协议原 ...
- LINQ 函数的实战演练测试
1.首先定义一个图书类.专门存放图书的属性信息. 代码如下: //Book.cs using System; namespace LinqTest { public class Book { pu ...
- ifram-locatione页面跳转
在涉及银行页面时,需要跳转到银行页面var accound = document.getElemntById('accound');$.ajax({ dataType:'json', type:'po ...
- UEFI模式下Win10和Linux双系统
一.准备 用Win自带的磁盘管理或者进PE分出一块空间来. 你必须要有一个U盘,然后使用软碟通或者ImageWriter把iso系统镜像文件烧录进去,这是比较传统的方法,但既然我们UEFI启动,那就根 ...