273 Integer to English Words 整数转换英文表示
将非负整数转换为其对应的英文表示,给定的输入是保证小于 231 - 1 的。
示例:
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
详见:https://leetcode.com/problems/integer-to-english-words/description/
Java实现:
每三位数一读。每读三位数的时候在三位数后面加上单位(Thousand, Million, or Billion)即可。其中的关键点是当读取的三位数是“0”,那么后面的单位就要舍弃。比如1000000,读第二个三位数是“000”,那么对应的单位Thousand是要舍弃的,否则就会变成One Million Thousand的错误结果。
class Solution {
//全局变量先把要用的英文存起来
String[] units = {"", " Thousand", " Million", " Billion"};
String[] num0To9 = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
String[] num10To19 = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] num10To90 = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public String numberToWords(int num) {
if (num == 0){
return "Zero";
}
String res = "";
int count = 0; //记录当前三位数下后面跟的单位
while (num > 0) {
String tmp = "";
tmp = units[count]; //记录当前三位数下后面跟的单位
int cur = num % 1000; //每三位一读,从后往前
String pre = convert(cur); //转化当前数字最后的三位数
if (pre == ""){
tmp = ""; //如果是"000",那么就等于什么都没发生,舍弃单位
}else{
tmp = convert(cur) + tmp; //否则结合结果和单位
}
if (res.length() != 0 && res.charAt(0) != ' '){ //处理一下加上单位的空格情况
res = tmp + " " + res;
}else{
res = tmp + res;
}
num = (num - num % 1000) / 1000; //处理往前三位数
count++;
}
return res;
}
//转化任意三位数
public String convert(int num) {
if (num == 0){
return "";
}
if (num < 10){
return num0To9[num];
}else if (num >= 10 && num <= 19){
return num10To19[num - 10];
}else if (num >= 20 && num <= 99) {
if (num % 10 == 0){
return num10To90[num / 10 - 1];
}else{
String s1 = num0To9[num%10];
String s2 = num10To90[num/10 - 1];
return s2 + " " + s1;
}
}
else {
if (num % 100 == 0){
return num0To9[num / 100] + " Hundred";
}else {
String tmp = convert(num % 100);
return convert(num - num % 100) + " " + tmp;
}
}
}
}
C++实现:
class Solution {
public:
string numberToWords(int num) {
string res = convertHundred(num % 1000);
vector<string> v = {"Thousand", "Million", "Billion"};
for (int i = 0; i < 3; ++i)
{
num /= 1000;
res = num % 1000 ? convertHundred(num % 1000) + " " + v[i] + " " + res : res;
}
while (res.back() == ' ')
{
res.pop_back();
}
return res.empty() ? "Zero" : res;
}
string convertHundred(int num) {
vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string res;
int a = num / 100, b = num % 100, c = num % 10;
res = b < 20 ? v1[b] : v2[b / 10] + (c ? " " + v1[c] : "");
if (a > 0)
{
res = v1[a] + " Hundred" + (b ? " " + res : "");
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4772780.html
273 Integer to English Words 整数转换英文表示的更多相关文章
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- Java实现 LeetCode 273 整数转换英文表示
273. 整数转换英文表示 将非负整数转换为其对应的英文表示.可以保证给定输入小于 231 - 1 . 示例 1: 输入: 123 输出: "One Hundred Twenty Three ...
- Leetcode 273.整数转换英文表示
整数转换英文表示 将非负整数转换为其对应的英文表示.可以保证给定输入小于 231 - 1 . 示例 1: 输入: 123 输出: "One Hundred Twenty Three" ...
- leetcode-【hard】273. Integer to English Words
题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 273. Integer to English Words
题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to ...
- 273. Integer to English Words数字转为单词
[抄题]: Convert a non-negative integer to its english words representation. Given input is guaranteed ...
随机推荐
- Thinkphp5.0 的视图view的模板布局
Thinkphp5.0 的视图view的模板布局 使用include,文件包含: <!-- 头部 --> <div class="header"> {inc ...
- 从零开始写STL-容器-list
从零开始写STL-容器-list List 是STL 中的链表容器,今天我们将通过阅读和实现list源码来解决一下问题: List内部的内存结构是如何实现的? 为什么List的插入复杂度为O(1)? ...
- cogs——2084. Asm.Def的基本算法
2084. Asm.Def的基本算法 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] “有句 ...
- python 时间四舍五入
假设时间格式为 YYYYMMDDhhmm , 比如201508010001 代表2015年8月1日0点01分. 现在有需求,要求一个start 和一个 end 变量的字符串 都是这种格式的时间. 需要 ...
- 百万级 TCP 长连接即时通讯框架 t-io
原文:http://www.t-io.org:9292/ https://www.oschina.net/p/t-io
- zabbix学习系列之QQ消息报警
安装依赖包 环境 Zabbix: 3.2 OS:Centos 安装依赖包 yum install lrzsz chrony gcc gcc-c++ git openssl-devel perl-Ext ...
- web开发常见性能优化方式
经常使用的高并发. 高性能web,数据库server. 1.html 静态化 : 如新闻频道更新的非常快,都是通过cms静态生成(门户,信息公布类型的站点,交互性高的如猫扑的大杂烩也是静态化,实时静 ...
- AE After Effect 如何替换和修改素材
替换素材:如图所示,相框外的人是成双成对的,相框里面的却只有一个人,我们想要把这个素材替换成两个人的.我们鼠标放到视频预览框的任何一个元素上面底部都会提示这是什么素材.比如我放到一个人的上面,则该人物 ...
- ABAP学习之旅——多种方式建立模块化功能
在ABAP中.有多种方法能够建立模块化的功能. 以下依次对其种三种进行介绍. 一. 使用子程序(Subroutine) 1. 基本的语法: FORM subname. ...
- MySQL中文參考手冊
非常好的中文手冊: 链接:http://www.sdau.edu.cn/support/mysq_doc/manual_toc.html