题目链接:https://leetcode.com/problems/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"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

思路:将其划分为几种情况递归处理,billion, million, thousand, hundred, ten, or others,并预先将数字对于的英文存储起来.

代码如下:

class Solution {
public:string numberToWords(int num) {if(num == 0) return "Zero";return convert(num).substr(1);}string convert(int num){if(num >= 1000000000) return convert(num/1000000000) + " Billion" + convert(num - 1000000000*(num/1000000000));if(num >= 1000000)return convert(num/1000000) + " Million" + convert(num - 1000000*(num/1000000));if(num >= 1000)return convert(num/1000) + " Thousand" + convert(num - 1000*(num/1000));if(num >= 100)return convert(num/100) + " Hundred" + convert(num - 100*(num/100));if(num >= 20)return " " + above20[num/10-2] + convert(num - 10*(num/10));if(num >= 1)return " " + below20[num-1];return "";}
private:string above20[8] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};string below20[19] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven","Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
};

参考:https://leetcode.com/discuss/57747/fairly-clear-4ms-c-solution

[leetcode] 273. Integer to English Words 解题报告相关推荐

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

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

  2. [leetcode] 273.Integer to English Words

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

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

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

  4. LeetCode第45场双周赛-解题报告

    LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...

  5. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

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

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

  7. leetcode 214. 最短回文串 解题报告

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

  8. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  9. [Leetcode] 74. Search a 2D Matrix 解题报告

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

最新文章

  1. PMP-【第10章 项目沟通管理】-2021-2-16(220页-231页)
  2. [mmu/cache]-MMU的寄存器学习
  3. GetCurrentProcessID、OpenProcessToken、LookupPrivilegeValue、AdjustTokenPrivileges
  4. 《零基础看得懂的C++入门教程 》——(7)小数组玩起来
  5. windows 编程随笔——输出文本WM_PAINT消息|有效矩形和无效矩形
  6. (31)FPGA面试题系统最高速度计算方法
  7. 微软 2018 Build 大会前瞻:AI、Azure、Windows 10 都在!
  8. Spark性能优化指南——基础篇【1】
  9. C#中,接口继承、基类继承中父类与基类的执行顺序
  10. vant 软键盘_H5页面 绝对定位元素被 软键盘弹出时顶起
  11. RSAC2020的PPT下载
  12. 输入一个字符串,判断它的所有字符中否全部是大写字母,如不是,统计小写字母个数,并将其转换成大写字母后输出
  13. 数显之家快讯:【SHIO世硕心语】一个人成功之前,需要失去五样东西!
  14. 计算偏相关系数和复相关系数
  15. java爱听音乐音乐播放器
  16. 不愧是阿里P8!深入理解Java虚拟机pdf百度云
  17. Kubernetes 之 二进制安装(二) 证书详解
  18. Lua不同类型变量做比较时的问题
  19. 打印端口用计算机名,打印机端口名改不了怎么解决 如何解决打印机端口名改不了问题...
  20. 右下角弹出广告 js,漂浮效果(兼容多浏览器)

热门文章

  1. ISO27001的认证周期及认证详细流程
  2. Heterogeneous Edge-Enhanced Graph Attention Network For Multi-Agent Trajectory Prediction(HEATConv)
  3. 删除文件出现 数据错误 循环冗余的解决办法
  4. 关于 sensor hdr 模式下不出图/出图异常的排查方法
  5. vue3+echarts5 中国地图+散点地图
  6. python列表写入txt文件_Python:将 list 写入一个 txt 文件
  7. 一文看透指令集、微架构、CPU
  8. 1药网三年亏损13亿元:股价再创新低,于刚、刘峻岭身价缩水八成
  9. oracle 桌面安装步骤,Oracle安装:OUI安装Oracle(图形界面安装)
  10. 关于SSD的一些日记