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. python之登录小程序

    # 登录操作 PassWord_list = [] Reset_pw = '*#*#' def account_login(): if PassWord_list == []: PassWord = ...

  2. Collection集合的功能及总结

    Collection集合是集合顶层接口,不能实例化 功能 1.添加功能 boolean add(Object obj):添加一个元素 boolean addAll(Collection c):添加一个 ...

  3. 各个浏览器显示版本(IE,火狐)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. sdk开发时,对外暴露的接口封装

    思考,用同步还是异步? 实质就是屏蔽一些东西,让使用者直接传参数 拿结果 而不用关心具体实现 eg.登陆接口 1.定义接口LoginCallBack,两个函数 请求成功和失败 public inter ...

  5. (转载)android:android.content.res.Resources$NotFoundException: String resource ID #..

    android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0 原因分析如下: 遇到这种 ...

  6. 5 Hbase

    # 大纲: * 认识 HBase * HBase 架构 * HBase读写流程   定义: *  HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用Hbase 技术可在廉价PC S ...

  7. laravel强大功能路由初探(二)

    目标当然是先输出helloworld 配置hosts文件和apache下的httpd-vhosts.conf, hosts:127.0.0.1  www.blog.com httpd-vhosts.c ...

  8. C# HttpWebReqeust和HttpWebResponse发送请求

    var request = (HttpWebRequest)WebRequest.Create("URL"); var data = Encoding.UTF8.GetBytes( ...

  9. 为什么要用base64编码

    1.需求 了解为什么要使用base64对数据编码 2.理由 因为传输二进制数据的时候,网络中间的有些路由会把ascii码中的不可见字符删了,导致数据不一致.一般也会对url进行base64编码 Whe ...

  10. 一堆LCT板子

    搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...