LeetCode 273. Integer to English Words
原题链接在这里:https://leetcode.com/problems/integer-to-english-words/
题目:
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
Example 1:
Input: 123
Output: "One Hundred Twenty Three"
Example 2:
Input: 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
题解:
没三个digit分成一个 unit, 用unitNumber 函数把这三位数换算成数字加上对应的unit.
Note: num = 1,000,000时不可以出现 "One Million Thousand"的情况,所以while循环时 要加上if(rest>0)的限定条件.
Time Complexity: O(n), n是unit的个数 Space: O(1).
AC Java:
 public class Solution {
     public String numberToWords(int num) {
         if(num == 0){
             return "Zero";
         }
         String res = "";
         String [] units = {"", " Thousand", " Million", " Billion"};
         int i = 0;
         while(num > 0){
             int unit = num%1000;
             if(unit > 0){ //error 1,000,000
                 res = unitNumber(unit) + units[i] + (res.length() == 0 ? "" : " "+res);
             }
             num = num/1000;
             i++;
         }
         return res;
     }
     private String unitNumber(int unit){
         String res = "";
         String [] ten = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
         String [] twenty ={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
         String [] hundred = {"Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
         int a = unit/100;
         int b = unit%100;
         int c = unit%10;
         if(a > 0){
             res = ten[a] + " Hundred";
         }
         if(b>=10 && b<20){
             if(res.length() != 0){
                 res = res + " ";
             }
             res = res + twenty[b%10];
             return res;
         }else if(b >= 20){
             if(res.length() != 0){
                 res = res + " ";
             }
             b = b/10;
             res = res + hundred[b];
         }
         if(c > 0){
             if(res.length() != 0){
                 res = res + " ";
             }
             res = res + ten[c];
         }
         return res;
     }
 }
LeetCode 273. Integer to English Words的更多相关文章
- leetcode@ [273] Integer to English Words (String & Math)
		https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ... 
- [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 ... 
- 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 ... 
- 【LeetCode】Integer to English Words 解题报告
		Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ... 
- 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 ... 
- [LC] 273. Integer to English Words
		Convert a non-negative integer to its english words representation. Given input is guaranteed to be ... 
随机推荐
- BBS项目架构
			数据库设计 用户表(用的是auth_user那张表,通过自定义表继承AbstractUser) phone 电话 avatar 头像 create_time 创建时间#外键 blog 一对一个人站点表 ... 
- git出现Invalid path
			今天换了电脑,我直接把整个仓库从电脑A复制到了电脑B,包括仓库下面的 .git 文件夹. 修改代码后我执行了一下 git add . 出现了一个报错 fatal: Invalid path 'D:/S ... 
- linux学习之路(一)--centos7安装JDK
			一.卸载centos自带jdk 1.rpm -qa | grep java 查看包含“java”关键字的安装包. 2.然后通过 rpm -e --nodeps 后面跟系统自带的jdk名 ... 
- 运输计划[二分答案 LCA 树上差分]
			也许更好的阅读体验 \(\mathcal{Description}\) 原题链接 概括一下题意 给一颗有\(n\)个点带边权的树,有\(m\)个询问,每次询问\(u,v\)两点间的权值和,你可以将树中 ... 
- hdu 1075 map的使用 字符串截取的常用手段 以及string getline 使用起来的注意事项
			首先说字符串的截取套路吧 用坐标一个一个的输入 用遍历的方式逐个去检查字符串中的字符是否为符合的情况 如果是的话 把该字符放入截取string 中 让后坐标前移 如果不是的话 截取结束 坐标初始化 然 ... 
- python实现ssh及sftp功能
			1.在Linux上我们通过scp命令实现主机间的文件传送,通过ssh实现远程登录 ,比如 我们经常使用的xshell远程登录工具,就是基础ssh协议实现window主机远程登录Linux主机 下面简单 ... 
- Vue指令之`v-model`和`双向数据绑定
			v-bind 只能实现数据的单向绑定,从 M 自动绑定到 V, 无法实现数据的双向绑定 <input type="text" v-bind:value="msg& ... 
- 关于get 和post 方法的比较
			地址:https://my.oschina.net/leejun2005/blog/136820 点击这里 
- Typora数学公式
			LaTeX编辑数学公式基本语法元素 LaTeX中的数学模式有两种形式: inline 和 display. 前者是指在正文插入行间数学公式,后者独立排列,可以有或没有编号. 行间公式(inline) ... 
- 团队高效率协作开发的秘密武器-APIDOC
			团队高效率协作开发的秘密武器 1.前言 在团队协作开发中,不知道各位有没有遇到这样的问题: l 新人接手了项目代码,因没有项目文档,只能靠追踪路由,寻读代码分析业务逻辑 l 前端同学写好了页面,苦等后 ... 
