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 ...
随机推荐
- springboot+idea+maven学习第一天(springboot入门,idea整合maven)
1.springboot简介 简化Spring 用用开发的一个框架: 真个S僻壤技术栈的一个大整合 j2ee开发的一站式解决方案 2.微服务 微服务:是一种架构风格 一个应用应该是一组小型服务:可以通 ...
- CF #367 DIV2 E
直接使用指针,交换时交换矩阵周围的指针即可. #include <iostream> #include <cstdio> #include <cstring> us ...
- 小贝_mysql 存储过程
存储过程 简要: 1.什么是存储过程 2.使用存储过程 一.存储过程 概念类似于函数,就是把一段代码封装起来.当要行这段代码的时候,能够通过调用该存储过程来实现.在封装的语句体里面.能够用if/els ...
- redux 存值 及 取值 的操作
项目目录 首先,一个基于React + Redux + React-Router的项目目录可以按照我下方的图片来构建: 其中assets目录用于存放项目的静态资源,如css/图片等,src目录则用于存 ...
- phpmywind教程:关于日期函数调用整理
近期群里一直在问phpmywind的日期函数怎么调用,今天抽出时间给大家整理出来. 以月/日格式显示: <?php echo MyDate('m-d', $row['posttime']); ? ...
- 05-图1. List Components (25)
05-图1. List Components (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue For a ...
- IIS网站无法启动,提示 另一个程序正在使用此文件
s还用netstat -ano命令,观察哪一个进程正在使用80端口,任务管理中勾选PID,看看是哪个程序,关掉即可
- UVA 213 Message Decoding 【模拟】
题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...
- ZOJ 3964 Yet Another Game of Stones Nim游戏变种
ZOJ3964 解题思路 此题的题意比较容易理解,可以简单的看着 Nim 博弈的变种.但问题在于 Alice 对第 i 堆石子的取法必须根据 bi 确定.所以如果这个问题能够归结到正常的 Nim 博弈 ...
- python time 时间模块
time():获取当前系统的时间戳ctime():以人类可读的方式打印当前系统时间sleep():接受一个参数,表示休眠时间 #!/usr/bin/env python #coding:utf8 im ...