问题描述:

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

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
方法一:自己写的,没有层次感,看起来不够清晰

 static String[][] enStr = {
{"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen",
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}, //1-19
{"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"} // 20-90
};
public String numberToWords(int num) {
if(num < 0) //非负整数
return null;
String english = "";
int remain = 0;
if(num / 20 == 0) {//1-19
if(num == 0) //num == 0单独处理
return "Zero";
return enStr[0][num];
} else if(num / 100 == 0){ //20-99
if(num % 10 == 0) //若是20,30,40,50,...,90
return enStr[1][num / 10];
else
return enStr[1][num / 10] + " " + enStr[0][num % 10];
} else if(num / 1000 == 0){//100-999
int h = num / 100;
remain = num % 100;
if(remain >= 20 && remain % 10 ==0){
english = enStr[0][h] + " Hundred " + enStr[1][remain / 10];
} else if(remain >= 20 && remain % 10 > 0){
english = enStr[0][h] + " Hundred " + enStr[1][remain / 10] + " " + enStr[0][remain % 10];
} else if(remain == 0){
english = enStr[0][h] + " Hundred";
} else {
english = enStr[0][h] + " Hundred " + enStr[0][remain];
}
return english;
} else if(num / 1000000 == 0){ //1000-999999
int th = num / 1000;
remain = num % 1000;
if(remain == 0){
return numberToWords(th) + " Thousand";
}
return numberToWords(th) + " Thousand " + numberToWords(remain);
} else if(num / 1000000000 == 0) {//1000000-999999999
int mi = num / 1000000;
remain = num % 1000000;
if(remain == 0){
return numberToWords(mi) + " Million";
}
return numberToWords(mi) + " Million " + numberToWords(remain);
} else { //1000000000-9999999999
int bi = num / 1000000000;
remain = num % 1000000000;
if(remain == 0){
return numberToWords( bi) + " Billion";
}
return numberToWords(bi) + " Billion " + numberToWords(remain);
}
}

方法二:代码整齐,参考别人的
 public String numberToWords(int num) {
if (num < 0) {
return "";
} //数字为0直接返回
if (num == 0) {
return "Zero";
} //左起段落
int segment1 = num / 1000000000; //段落1:十亿位-千亿位
int segment2 = num % 1000000000 / 1000000; //段落2:百万位-亿位
int segment3 = num % 1000000 / 1000; //段落3:千位-十万位
int segment4 = num % 1000; //段落4:个位-百位 String result = ""; if (segment1 > 0) {
result += numToWordsLessThan1000(segment1) + " " + "Billion";
}
if (segment2 > 0) {
result += numToWordsLessThan1000(segment2) + " " + "Million";
}
if (segment3 > 0) {
result += numToWordsLessThan1000(segment3) + " " + "Thousand";
}
if (segment4 > 0) {
result += numToWordsLessThan1000(segment4);
} return result.trim(); //去掉字符串首尾的空格
} private String numToWordsLessThan1000(int num) { if (num == 0 || num >= 1000) {
return "";
} String result = "";
if (num >= 100) {
result += numToWordsBase(num / 100) + " " + "Hundred";
}
num = num % 100;
if (num > 20) {
result += numToWordsBase(num / 10 * 10);
if (num % 10 != 0) {
result += numToWordsBase(num % 10);
}
} else if (num > 0) {
result += numToWordsBase(num);
} return result;
} private String numToWordsBase(int num) {
String result = " ";
switch (num) {
case 1: result += "One"; break;
case 2: result += "Two"; break;
case 3: result += "Three"; break;
case 4: result += "Four"; break;
case 5: result += "Five"; break;
case 6: result += "Six"; break;
case 7: result += "Seven"; break;
case 8: result += "Eight"; break;
case 9: result += "Nine"; break;
case 10: result += "Ten"; break;
case 11: result += "Eleven"; break;
case 12: result += "Twelve"; break;
case 13: result += "Thirteen"; break;
case 14: result += "Fourteen"; break;
case 15: result += "Fifteen"; break;
case 16: result += "Sixteen"; break;
case 17: result += "Seventeen"; break;
case 18: result += "Eighteen"; break;
case 19: result += "Nineteen"; break;
case 20: result += "Twenty"; break;
case 30: result += "Thirty"; break;
case 40: result += "Forty"; break;
case 50: result += "Fifty"; break;
case 60: result += "Sixty"; break;
case 70: result += "Seventy"; break;
case 80: result += "Eighty"; break;
case 90: result += "Ninety"; break;
}
return result;
}

Integer to English words leetcode java的更多相关文章

  1. 【LeetCode】Integer to English Words 解题报告

    Integer to English Words [LeetCode] https://leetcode.com/problems/integer-to-english-words/ Total Ac ...

  2. 【LeetCode】273. Integer to English Words

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

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

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

  4. Mybatis的失误填坑-java.lang.Integer cannot be cast to java.lang.String

    Mybatis的CRUD小Demo 为方便查看每次的增删改结果,封装了查询,用来显示数据库的记录: public static void showInfo(){ SqlSession session ...

  5. “无效数字” ;java.lang.Integer cannot be cast to java.lang.String

    今天页面上突然查询不出数据,大致的sql语句是 select xx ,xxx from table a where a.lrmb in ( 6101060033, 61010503300, 61016 ...

  6. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  7. JSONObject转换Int类型--java.lang.Integer cannot be cast to java.lang.String

    参数 params={"abc":0} JSONObject转换Int类型 JSONObject json = JSONObject.fromObject(params); if ...

  8. java 解决 java.lang.Integer cannot be cast to java.lang.String

    1.在执行代码打印map的value时,提示错误java.lang.Integer cannot be  cast to java.lang.String,这个错误很明显是类型转换错误 查看表字段的数 ...

  9. java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

    1.错误描写叙述 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String service. ...

随机推荐

  1. JVM启动参数大全

    java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足, ...

  2. ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

    题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multi ...

  3. P3261 [JLOI2015]城池攻占

    思路 左偏树维护每个骑士的战斗力和加入的深度(因为只能向上跳) 注意做乘法的时候加法tag会受到影响 代码 #include <cstdio> #include <algorithm ...

  4. webpack用 babel将ES6转译ES5

    webpack webpack.config.js配置文件 module.exports = { entry: './es6.js', // 入口文件路径 output: { filename: &q ...

  5. Bytom国密网说明和指南

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 国密算法 ...

  6. Servlet简介及其生命周期详解

    简介        Servlet生命周期,即阐述Servlet从产生到毁灭的整个过程.         在Servlet产生到消亡的过程中,有三个生命周期函数,初始化方法init(),处理客户请求的 ...

  7. ngnix简介以及如何实现负载均衡原理

    1 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况.那么负载均衡的前提就是要有多台服务器才能实现, ...

  8. Gym 101617J Treasure Map(bfs暴力)

    http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...

  9. HDU 3526 Computer Assembling(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件 ...

  10. 用命令bat打开某个文件或文件夹

    打开文件或文件夹可以用start命令,start命令会根据文件关联的程序自动调用关联的程序打开文件和文件夹. 可以用记事本写如下命令,之后改扩展名为.bat即可 打开文件夹 start "& ...