将非负整数转换为其对应的英文表示,给定的输入是保证小于 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. redhat 6 配置 yum 源

    1.删除redhat原有的yum rpm -aq|grep yum|xargs rpm -e --nodeps 2.下载yum安装文件 注意,如果下载时找不到文件,就登录到:http://mirror ...

  2. 牛腩新闻系统(一)——UML、数据库设计

    牛腩新闻系统(一)--UML.数据库设计 一.初识牛腩系统 牛腩(Brisket)即牛腹部及靠近牛肋处的松软肌肉,是指带有筋.肉.油花的肉 块.这是一种统称. 若依部位来分,牛身上很多地方的肉都能够叫 ...

  3. javascript statically scope

    在javascript 里面, 函数中使用的未定义的变量,会默认变为全局的变量. 而通过 var 这个关键字定义的变量,就是局部变量. As far as the output is concerne ...

  4. ajax请求同步与异步的区别

    //同步请求 $.ajax({    type:'post', url:"<c:url value='/device/org/' />"+val, data:{'org ...

  5. 自己动手写shell命令之ls -R1fF

    ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...

  6. java7新特性之Diamond syntax

    java7新特性之Diamond syntax Java 7 also introduces a change that means less typing for you when dealing ...

  7. 2016/1/20 笔记 1, 包 引入 static 已经补充到类里 2,继承

    继承  1,关键字 extends       2,子类自动继承父类非私有的属性和方法 也叫成员变量 成员方法       3,super代表的是父类 调用父类的方法 默认在构造函数中生成      ...

  8. Spring Boot Controller

    接上篇文章.HelloWorld程序中我们已经创建了一个HellController,里面包括了响应JSON的方法.本文针对Controller再做一下解说. 回想上篇文章,我们在Controller ...

  9. 容器HashSet原理(学习)

    一.概述 使用HashMap存储,非线程安全: 二.实现 HashSet 底层使用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,相关 HashSet 的操作,基本上都是直接调 ...

  10. 【Codevs 1376】帕秋莉•诺蕾姬

    http://codevs.cn/problem/1376/ 枚举修改哪两位,将sum减去之前位置的数+交换之后  %m==0即可 预处理26的次方+O(n^2) // <1376.cpp> ...