将非负整数转换为其对应的英文表示,给定的输入是保证小于 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 整数转换英文表示的更多相关文章

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

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

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

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

  3. Java实现 LeetCode 273 整数转换英文表示

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

  4. Leetcode 273.整数转换英文表示

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

  5. leetcode-【hard】273. Integer to English Words

    题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...

  6. 【LeetCode】273. Integer to English Words

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

  7. [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words

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

  8. 273. Integer to English Words

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

  9. 273. Integer to English Words数字转为单词

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

随机推荐

  1. QT-Embedded-4.5.3在海思35xx上移植

    QT4.5.3在海思3520A上移植步骤-修订版 2015年3月29日星期日, 16:59:03 1.首先要保证已经安装了海思的交叉编译器: #arm-hi  + Tab key to show wh ...

  2. SpringBoot学习day01

    SpringBoot目的在于创建和启动新的基于Spring框架的项目.SpringBoot会选择最合适的Spring子项目和第三方开源库进行整合.大部分SpringBoot应用只需要非常少量的配置就可 ...

  3. 详解ORACLE数据库的分区表

    此文从以下几个方面来整理关于分区表的概念及操作:    1.表空间及分区表的概念    2.表分区的具体作用    3.表分区的优缺点    4.表分区的几种类型及操作方法    5.对表分区的维护性 ...

  4. mybatis collection标签和association标签(一对多,一对一)转载

    mybatis 一对一与一对多collection和association的使用   在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.associati ...

  5. java监控工具jconsole

    jconsole可以监控本地和远程进程 jvisualvm

  6. cmd启动Oracle服务和监听服务

    启动数据库服务 net start oracleserviceorcl 启动数据库监听 lsnrctl start

  7. 关于微软网站维护培训时遇到的issues:​​​What is WEDCS?

    WEDCS (pronounced “wed-ex”), the Web Events Data Collection Service, is a system for collecting, com ...

  8. hdu 5256 序列变换

    最长上升子序列 nlogn;也是从别人的博客学来的 #include<iostream> #include<algorithm> #define maxn 100000+5 u ...

  9. 【bzoj2003】[Hnoi2010]Matrix 矩阵

    首先可以知道,如果已知第一行和第一列的数,那么可以很容易的计算出其余的数.进一步的,如果笔算将每个数的表达式写出可以得出如下结论: 第i行的第j个数(i>1,j>1)只与(1,1),(i, ...

  10. js鼠标事情

    js鼠标事情 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...