题目链接: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. 给你一个K8S的“发行版”
  2. 把有无线网卡的机子当作无线ap无需任何软件
  3. win7系统屏幕键盘打开教程
  4. 锁到底是一种怎样的存在?
  5. Soap、Http、TCP/IP 三个基本的通讯协议有什么区别?
  6. Java实现在线语音识别
  7. 码农如何写好一封邮件/1
  8. Line RichEdit类
  9. 天气和气象数据网站集合
  10. 在线Cron表达生成器(定时任务时间控制)
  11. 全体是合同,多学学吧。当前少吃亏。
  12. 讨论实现Windows资源管理器的简单方式
  13. [转] iOS 开发者应该知道的 ARM 结构(转自apple4us)
  14. Android解析SRT字幕文件
  15. 西瓜文案:水果西瓜的文案,水果文案西瓜
  16. 太原师范学院计算机考研率,太原师范学院怎么样(太原师范学院考研率)
  17. Spring Bean作用域与生命周期
  18. Mybatis的一对多映射
  19. 计算机系统基础:计算机系统概述
  20. 网络的几种分层体系结构

热门文章

  1. Bugku — never give up (PHP 黑魔法函数绕过)— writeup
  2. 中压开关设备应用现状及发展
  3. 刀具项目优化之watch
  4. 人类天才智商最高排行榜
  5. java 堆内存使用情况_java语言:JVM堆内存使用率持续上升如何排查
  6. 目标检测中的Bounding Box Regression Loss
  7. 全国计算机设计水平大赛,喜报|“全国计算机设计大赛”又被传媒学子斩获大奖!...
  8. Linux下Vim编辑Gcc编译实现局域网聊天系统
  9. 广告威客任务平台源码投票抖音点赞兼职悬赏/蚂蚁帮扶众人帮/可封装App带教程
  10. Python+Vue计算机毕业设计威客任务平台系统083ul(源码+程序+LW+部署)