题目:

273. 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"

答案:

按着自己用英文阅读整数的顺序来就行:

1、这道题目不是难在思路上,而是难在考虑的情况比较多,比较多细节,一不小心就有小bug,需要思路清晰,逻辑清晰

2、英文中用来表示整数的单词并不多,分类:

1-9(个位数),11-19(特殊的十位数),10-90(十位数),100(hundred),1000(thousand),1000000(million),1000000000(billion)

3、10这个十位数比较特别,只有在后两位完全为10时,才用ten

4、注意空格,后面没有数字了,就不能加空格了

5、每千位数跟后面的千位数(如果不为空,即0000000……)要有空格

6、注意不要乱加前缀空格和后缀空格

将上面的细节注意了就AC啦

代码:

 #include <vector>
#include <string> using std::vector;
using std::string; string n2s[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
string g2s[] = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
string t2s[] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; class Solution {
private:
vector<string> ans; public:
string numberToWords(int num) {
if(num == )
{
return "Zero";
} unsigned int rem;
unsigned int one;
unsigned int two;
unsigned int three; string perAns;
ans.clear(); do
{
//获取余数
rem = num % ;
num = num /; //存储每次计算的结果
perAns.clear();
perAns = ""; //一千以内的数,取每一位
one = rem % ;
two = (rem / ) % ;
three = (rem / ) % ; if(three != )
{
perAns += n2s[three - ];
perAns += " Hundred";
} //如果后两位都为0,那么后面就没有数了,也就不需要加空格
if(perAns != "" && (one != || two != ))
{
perAns += " ";
} if(two == )
{
//考虑最后一位是否为0的情况,因最后两位为10时,需要用“ten”
//否则就是十位数
if(one == )
{
perAns += t2s[];
}else
{
perAns += g2s[one - ];
}
//考虑two不为0,不为1,则按一般规则去计算
}else if(two != )
{
perAns += t2s[two - ]; if(one != )
{
perAns += " ";//如果最后一位不为0,需要在其前面加空格
perAns += n2s[one - ];
}
//考虑two为0的情况
}else
{
if(one != )
{
perAns += n2s[one - ];
}
} //将结果存储到ans中,ans中的答案是以逆序形式存储了每个千位数
ans.push_back(perAns);
}while(num != ); string result = "";
unsigned int len = ans.size();
//len最大长度为4,考虑每种位数就行,这里用的时候就会发现,合理使用goto,程序逻辑会很清晰
switch(len)
{
case :goto three;break;
case :goto two;break;
case :goto one;break;
case :goto zero;break;
} three:
if(ans[] != "")
{
result += ans[];
result += " Billion"; if(ans[] != "" || ans[] != "" || ans[] != "")
{
result += " ";
}
} two:
if(ans[] != "")
{
result += ans[];
result += " Million"; if(ans[] != "" || ans[] != "")
{
result += " ";
}
} one:
if(ans[] != "")
{
result += ans[];
result += " Thousand"; if(ans[] != "")
{
result += " ";
}
} zero:
if(ans[] != "")
{
result += ans[];
} return result;
}
};

说明: 合理使用goto会取到很好的效果哦

leetcode-【hard】273. Integer to English Words的更多相关文章

  1. 【LeetCode】273. Integer to English Words

    Integer to English Words Convert a non-negative integer to its english words representation. Given i ...

  2. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  3. LeetCode: 【L4】N-Queens 解题报告

    [L4]N-Queens 解题报告 N-Queens Total Accepted: 16418 Total Submissions: 63309 My Submissions The n-queen ...

  4. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  5. [LeetCode] 273. Integer to English Words 整数转为英文单词

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

  6. LeetCode 273. Integer to English Words

    原题链接在这里:https://leetcode.com/problems/integer-to-english-words/description/ 题目: Convert a non-negati ...

  7. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

  8. [leetcode]273. Integer to English Words 整数转英文单词

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

  9. 【Leetcode】【Easy】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

随机推荐

  1. JVM加载类的过程,双亲委派机制中的方法

    JVM加载类的过程: 1)JVM中类的整个生命周期: 加载=>验证=>准备=>解析=>初始化=>使用=>卸载  1.1.加载 类的加载阶段,主要是获取定义此类的二进 ...

  2. clang编译器简介

    本文部分内容引用: 中文维基百科. 结构化编译器前端--clang介绍. 什么是clang编译器? clang是LLVM编译器工具集的一个用于编译C.C++.Objective-C的前端.LLVM项目 ...

  3. 分析案例:应用服务器W3WP进程CPU持续超过百分之九十(Oracle客户端Bug)

    问题描述: 项目反馈应用负载的其中一台服务器业务操作的响应非常慢,登录该服务器发现W3WP进程CPU持续超过90%,哪怕在业务低峰期也是如此?远程查看后发现该应用服务器承载的请求确实很低,why??? ...

  4. 使用jQuery Autocomplete(自动完成)插件

    jQuery 的Autocomplete(自动完成.自动填充)插件有不少,但比较下来我感觉,还是bassistance.de 的比较强大,我们就来写一些代码感受一下. 最简单的Autocomplete ...

  5. LB负载均衡之Nginx-Proxy

    LB负载均衡之Nginx-Proxy Nginx 反向代理及负载均衡引用实战 Nginx反向代理的原理优点:      Nginx proxy(反向代理)作为Nginx的重要功能,使用nginx pr ...

  6. html: title换行方法 如a链接标签内title属性鼠标悬停提示内容换行

    换行代码符合分别为: “&#;”和“&#;” <a href="0.shtml" title="第一排 第二排 第三排">title ...

  7. Markdown 软件

    <<github.css>> Markdown 软件 :Typora / Haroopad / Markdownpad2 Markdownpad2软件注册及下载地址 邮箱地址: ...

  8. (PHP)程序中如何判断当前用户终端是手机等移动终端

    推荐: Mobile-Detect:https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php Detect ...

  9. windows服务(installutil.exe)报错。异常来自 HRESULT:0x80131515

    报错如下: System.IO.FileLoadException: 未能加载文件或程序集“file:///G:\WindowsService1\bin\Debug\WindowsService1.e ...

  10. vbox丢失*MultiArch_amd64.msi或者*MultiArch_x86.msi重新制作

    1.去官网下载你缺少这个文件的那个版本的exe文件. 2.下载7-zip软件(就在百度下载,比较小). 3.用7-zip解压这个exe.会出现一个文件夹,大概名字为VirtualBox-4.3.12- ...