Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231−12^{31} - 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”

解题思路

略。

实现代码

// Runtime: 8 ms
class Solution {
public:string numberToWords(int num) {return num2str(num);}private:string num2str(int num){string res = "";if (num <= 19){res += nums[num];}else if (num <= 99){res += tens[num / 10];if (num % 10 != 0){res += " ";res += num2str(num % 10);}}else if (num <= 999){res += num2str(num / 100);res += " Hundred";if (num % 100 != 0){res += " ";res += num2str(num % 100);}}else if (num <= 999999){res += num2str(num / 1000);res += " Thousand";if (num % 1000 != 0){res += " ";res += num2str(num % 1000);}}else if (num <= 999999999){res += num2str(num / 1000000);res += " Million";if (num % 1000000 != 0){res += " ";res += num2str(num % 1000000);}}else if (num <= 999999999999){res += num2str(num / 1000000000);res += " Billion";if (num % 1000000000 != 0){res += " ";res += num2str(num % 1000000000);}}return res;}string tens[10] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};string nums[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight","Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"};
};

[LeetCode] Integer to English Words相关推荐

  1. LeetCode Integer to English Words(整数转化为英文字母)

    题意:给出一下正整数,输出英文表示 思路:每三个数一组,因为最多有三组,对应的分别为Billion,Million,Thousand.然后针对每组的数输出 代码如下: public class Sol ...

  2. LeetCode第一刷--leetcode提交格式介绍与273. Integer to English Words

    第一次玩Leetcode,对代码提交格式不了解,提交了一上午,略尴尬 leetcode确实有很多有意思的地方,不像以前做各个高校ACM练习,leetcode会给出错误信息(哪个数据的错了),也会提供测 ...

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

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/52756494 Subject 出处:https://leetcode.com/prob ...

  4. [leetcode] 273. Integer to English Words 解题报告

    题目链接:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its e ...

  5. [leetcode] 273.Integer to English Words

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

  6. LeetCode Integer Replacement

    题意:给出一个正数n,如果n为偶数则n = n /2,如果n为奇数,则n=n + 1或者n=n-1,求其变为1所需要的最少步数. 思路:刚开始用记忆化的dp,当数值比较大时,提示栈溢出.在判断是n=n ...

  7. 273 Integer to English Words 整数转换英文表示

    将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的. 示例: 123 -> "One Hundred Twenty Three" 12345 -> ...

  8. [LeetCode]Integer to Roman

    题目描述:(链接) Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

  9. LeetCode Integer Break(整数拆分问题)

    题意:给出一个大于等于2的正整数,将其拆分成若干个整数的和,使得其积最大 思路:在大于4的情况下,将其拆分成若干个3的和,这种情况下积是最大的 代码如下: public class Solution ...

最新文章

  1. 系统进程管理及用户登陆监控
  2. [译] NSCollectionView 入门教程
  3. Ubuntu下Git服务端搭建
  4. 论文盘点:CVPR 2019 - 文本检测专题
  5. 【转】增强 scite 编辑器的代码提示功能
  6. 戴帽子对眼睛是否有好处
  7. SAP ERP里如何创建一个新的material类型
  8. ROS笔记(17) Gazebo
  9. cognos 样例 oracle,cognos10.2 sample(ORACLE学样例)
  10. git 如何删除缓存的远程分支列表
  11. junit 生成html报告,gradle – 如何为JUnit 5测试创建HTML报告?
  12. H2080刷卡门禁一体机使用说明书 誉诚AD281A-T楼宇主机说明书
  13. Maven最新版的下载与安装教程(详细教程)
  14. 人口logistic模型公式_数学建模logistic人口增长模型
  15. 遗传算法(GA)求解车辆路径问题(VRP)——matlab实现
  16. mysql自动全量更新表_MySQL数据库自动全量备份脚本
  17. mac空格代表图标_最糟糕的图标代表功能
  18. 图文并茂讲VLAN,让你看一遍就理解VLAN
  19. 该网站服务器出错了怎么回事,该网站服务器出错了是什么意思(图文)
  20. 电话拨键号码(DTMF信号)识别

热门文章

  1. 卧加刀库回参考点_840D卧加回装后刀库如何设置
  2. Lisa美语的创始人Lisa Mojsin博士的微信公众号
  3. 计算机毕业设计php的软件推销销售网站
  4. python表情包斗图_Python爬取 斗图表情,让你成为斗图大佬
  5. 单数复数php单元格背景颜色,如何自定义分组表视图单元格的背景/边框颜色?...
  6. XBee zigbee 使用指南--- Zigbee无线数据传输示例
  7. 教你一键解除锁屏模式
  8. 我,ChatGPT,站在谷歌的肩膀上,让谷歌紧张
  9. 换了新的工具–X31
  10. 财务分析不再是难题,Smartbi教你财务数据人都该学习的分析方法