Integer to English Words

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"

Show Hint

 先把数字分割成4个三分位处理。前3个三分位分别加上Billion,Million,Thousand。
三分位处理函数为Helper()
class Solution {
public:
string Helper(string numStr, string dict1[], string dict2[], string dict3[])
{
int num = atoi(numStr.c_str());
if(num == )
return "";
else
{
string ret = "";
int hundred = numStr[] - '';
int ten = numStr[] - '';
int one = numStr[] - ''; if(hundred != )
ret += dict1[hundred-] + " Hundred"; if(ten == )
{
if(one == )
return ret;
else
return (ret=="")?(dict1[one-]):(ret+" "+dict1[one-]);
}
else if(ten == )
{
return (ret=="")?(dict2[one]):(ret+" "+dict2[one]);
}
else
{
if(one == )
return (ret=="")?(dict3[ten-]):(ret+" "+dict3[ten-]);
else
return (ret=="")?(dict3[ten-]+" "+dict1[one-]):(ret+" "+dict3[ten-]+" "+dict1[one-]);
}
}
}
string numberToWords(int num) {
if(num == )
return "Zero";
else
{
string ret = "";
string dict1[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
string dict2[] = {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
"Eighteen","Nineteen"};
string dict3[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; string numStr = to_string(num);
int len = numStr.size();
string padding = string(-len, '');
numStr = padding + numStr; string billionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(billionStr != "")
ret += billionStr + " Billion"; string millionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(millionStr != "")
ret += (ret=="")?(millionStr+" Million"):(" "+millionStr+" Million"); string thousandStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(thousandStr != "")
ret += (ret=="")?(thousandStr+" Thousand"):(" "+thousandStr+" Thousand"); string oneStr = Helper(numStr.substr(,), dict1, dict2, dict3);
if(oneStr != "")
ret += (ret=="")?(oneStr):(" "+oneStr);
return ret;
}
}
};

【LeetCode】273. Integer to English Words的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  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】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  4. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  5. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  6. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  7. 【leetcode】Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...

  8. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  9. 【leetcode】12. Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

随机推荐

  1. javaBean与Map<String,Object>互转

    背景:有时候想不通阿帕奇的BeanUtils是怎么进行map和Bean互相转化的. 工作闲暇之余,自己写个一小段代码,一探究竟,试试才发现,原来一切并非我们想的那么什么和复杂. 注:这里只是简单实例, ...

  2. React Native填坑之旅--ListView篇

    列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...

  3. iOS设置UISearchBar光标的颜色

    [[UISearchBar appearance] setTintColor:[UIColor blackColor]];

  4. 2015年9月10-11日,杨学明老师《IPD DRY RUN》专题培训在武汉某上市企业成功举办!

    2015-9-10~11日,杨学明老师为武汉著名的光通信企业某上市公司实施了为期两天的“IPD DRY RUN”,开班前,该公司三个项目团队的负责人先后发言,烽火PMO部门领导和公开研发部网管系统的领 ...

  5. cygwin下清屏的三种方法

    1. 做一个clear脚本,放到/bin下去 $vim /bin/clear #!/bin/bash cmd /c cls 2. ctrl + L 3. 在cygwind中install ncurse ...

  6. SQLSERVER 里经常看到的CACHE STORES是神马东东?

    SQLSERVER 里经常看到的CACHE STORES是神马东东? 当我们在SSMS里执行下面的SQL语句清空SQLSERVER的缓存的时候,我们会在SQL ERRORLOG里看到一些信息 DBCC ...

  7. iOS 重大新漏洞:可绕开苹果审核机制

    iOS 是目前最为安全可靠的移动平台,但既然是软件就不会是无坚不摧的.乔治亚技术信息安全中心 (Georgia Tech Information Security Center)的研究员不久前声称,他 ...

  8. node-webkit教程(14)禁用缓存

    1.在开发者工具中禁用缓存 上面这张图,是在node-webkit 中 在开发工具中配置禁用缓存的选项. 使用这个选项可以有效的禁用所有页面缓存. 2. 在配置文件中,配置webkit 缓存禁用和启用 ...

  9. AWK文本处理工具(Linux)

    AWK文本处理工具(Linux) PS:刚开始实习,就给了个处理百万级别数据的任务,以前学过SHELL的一些东西sed/awk之类的处理,但是也没有具体的应用,只是在10几行10几列的小数据操作过,所 ...

  10. 我们可以用SharePoint做什么

    前言 不知不觉作为一个SharePoint的开发人员若干年了,从SharePoint api 开始学习,到了解SharePoint的结构,逐渐一点点了解sharepoint的体系:从SharePoin ...