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,

  1. 123 -> "One Hundred Twenty Three"
  2. 12345 -> "Twelve Thousand Three Hundred Forty Five"
  3. 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Show Hint

 先把数字分割成4个三分位处理。前3个三分位分别加上Billion,Million,Thousand。
三分位处理函数为Helper()
  1. class Solution {
  2. public:
  3. string Helper(string numStr, string dict1[], string dict2[], string dict3[])
  4. {
  5. int num = atoi(numStr.c_str());
  6. if(num == )
  7. return "";
  8. else
  9. {
  10. string ret = "";
  11. int hundred = numStr[] - '';
  12. int ten = numStr[] - '';
  13. int one = numStr[] - '';
  14.  
  15. if(hundred != )
  16. ret += dict1[hundred-] + " Hundred";
  17.  
  18. if(ten == )
  19. {
  20. if(one == )
  21. return ret;
  22. else
  23. return (ret=="")?(dict1[one-]):(ret+" "+dict1[one-]);
  24. }
  25. else if(ten == )
  26. {
  27. return (ret=="")?(dict2[one]):(ret+" "+dict2[one]);
  28. }
  29. else
  30. {
  31. if(one == )
  32. return (ret=="")?(dict3[ten-]):(ret+" "+dict3[ten-]);
  33. else
  34. return (ret=="")?(dict3[ten-]+" "+dict1[one-]):(ret+" "+dict3[ten-]+" "+dict1[one-]);
  35. }
  36. }
  37. }
  38. string numberToWords(int num) {
  39. if(num == )
  40. return "Zero";
  41. else
  42. {
  43. string ret = "";
  44. string dict1[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
  45. string dict2[] = {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen",
  46. "Eighteen","Nineteen"};
  47. string dict3[] = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
  48.  
  49. string numStr = to_string(num);
  50. int len = numStr.size();
  51. string padding = string(-len, '');
  52. numStr = padding + numStr;
  53.  
  54. string billionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
  55. if(billionStr != "")
  56. ret += billionStr + " Billion";
  57.  
  58. string millionStr = Helper(numStr.substr(,), dict1, dict2, dict3);
  59. if(millionStr != "")
  60. ret += (ret=="")?(millionStr+" Million"):(" "+millionStr+" Million");
  61.  
  62. string thousandStr = Helper(numStr.substr(,), dict1, dict2, dict3);
  63. if(thousandStr != "")
  64. ret += (ret=="")?(thousandStr+" Thousand"):(" "+thousandStr+" Thousand");
  65.  
  66. string oneStr = Helper(numStr.substr(,), dict1, dict2, dict3);
  67. if(oneStr != "")
  68. ret += (ret=="")?(oneStr):(" "+oneStr);
  69. return ret;
  70. }
  71. }
  72. };

【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. Python缩进

    今天练习代码的时候发现一个问题,练习类,我在notepad++上写的代码运行后,复制到pycharm上运行然后报错,看代码 #---coding:utf-8--- #定义一个Person类然后实例化 ...

  2. mysql 常用配置

    1. 帐号不允许从远程登陆,只能在localhost 这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从“loc ...

  3. Ubuntu下制作ISO文件

    利用Ubuntu自带的命令mkisofs就可以制作iso文件,具体方法如下: 1.   如果你是直接从cd压制iso文件的,执行 sudo umount /dev/cdromdd if=/dev/cd ...

  4. ymPrompt消息组件

    <script src="jb51.net/prompt/jquery-1.7.1.js" type="text/javascript"></ ...

  5. GROUP BY,WHERE,HAVING之间的区别和用法

      GROUP BY,WHERE,HAVING之间的区别和用法 分类: Oracle学习2009-11-01 23:40 21963人阅读 评论(6) 收藏 举报 mathmanagersql数据库m ...

  6. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  7. mysql 5.6并行复制事件分发机制

    并行复制相关线程 在MySQL 5.6并行复制中,当设置set global slave_parallel_workers=2时,共有4个复制相关的线程,如下: +----+------------- ...

  8. Windows Server 2008 64 位 IIS7.5 ASP.NET MVC4 发布问题

    问题描述: 环境与配置: ASP.NET MVC 4 WINDOWS SERVER 2008 64 位 应用程序池是选择的 .NET 4.0 与经典模式   在新建一个MVC 4 项目发现到服务器上后 ...

  9. JsCss笔记

    1.  &= 不应该在 Bool 型变量中使用. a &= b  对于Js来说是:  a = a & b ;  a 本来是 bool , &= 之后就变成了 Int. ...

  10. 在Win7 环境使用Java API 上传文件到 Hadoop2.x HDFS 问题统计

    问题一: org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security .AccessControlExc ...