273 Integer to English Words 整数转换英文表示
2024-10-20 19:55:32
将非负整数转换为其对应的英文表示,给定的输入是保证小于 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
最新文章
- hibernate学习(6)——加载策略(优化)
- EBS learning history (to know about the environment)
- .net web弹出对话框
- Mapreduce读取Hbase表,写数据到一个Hbase表中
- 1041: [HAOI2008]圆上的整点 - BZOJ
- [topcoder]BusinessTasks
- [hadoop]Cannot create directory /mdrill/tablelist/fact_seller_all_d. Name node is in safe mode.
- x0vncserver Fatal server error: no screens found
- .NET单元测试艺术(2) - 第一个单元测试
- EntityFramework Core 1.1有哪些新特性呢?我们需要知道
- UML类图中的关系和表示方法
- An express train to reveries
- postman接口测试系列:接口参数化和参数的传递
- JavaScript中创建对象的几种模式
- python学习小总结(列表、元组、字典、集合、字符串)
- Maven模块化开发
- [leetcode]Decode Ways @ Python
- JS代理模式实现图片预加载
- Ubuntu18.04下使用Blender进行视频格式转换
- C#--类之隐藏基类的成员