原题链接:https://leetcode.com/problems/integer-to-english-words/
1. Description
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"
2. Solution
My Solution is as same as the LeetCode official Solution: https://leetcode.com/problems/integer-to-english-words/solution/
We need to split the number by 3-digits parts. Every 3-digits parts split by two parts: the digit of hundred and the rest.
The numbers between 11 and 20 need to be considered separately.
It is an easy but quite sticky problem. Be careful, good luck!
code
class Solution {
public String numberToWords(int num) {
//First,check if num is zero
if(num == 0){
return "Zero";
}
//simplify the problem:
//split the Interger , every parts has 3 digits
//subproblem: convert 3 digits integer to English words.
int billion = num / 1000000000;
int million = (num - (billion * 1000000000) ) / 1000000;
int thousand= (num - (billion * 1000000000) -(million * 1000000) ) / 1000;
int ones = num - (billion * 1000000000) - (million * 1000000) - (thousand * 1000);
String ans = "";
if(billion != 0){
ans += threedigits(billion) + " Billion";
}
if(million != 0){
if( !ans.isEmpty() ){
ans += " ";
}
ans += threedigits(million) + " Million";
}
if(thousand != 0){
if( !ans.isEmpty() ){
ans += " ";
}
ans += threedigits(thousand) + " Thousand";
}
if(ones != 0){
if( !ans.isEmpty() ){
ans += " ";
}
ans += threedigits(ones);
}
return ans;
}
// n>100 -> oneDigit(n/100) , twoDigit(n%100)
// n%100 == 0 -> oneDigit(n/100)
// n<100 -> twoDigit(n%100)
public String threedigits(int n){
int hundred = n/100;
int rest = n%100;
String s = "";
if(hundred != 0 && rest != 0){
return s + oneDigit(hundred)+" Hundred "+twoDigit(rest);
}else if(hundred != 0 && rest ==0){
return s + oneDigit(hundred)+" Hundred";
}else{
return twoDigit(rest);
}
}
//11-19 -> betweenTenTwenty(n)
//21-99 -> tenDigit(n%10) , oneDigit(n/10)
//the digits of one is 0 -> ten(n)
public String twoDigit(int n){
int ten = n/10;
int one = n%10;
if(ten == 0){
return oneDigit(one);
}else{
if(one == 0){
return ten(ten);
}else{
return ten == 1 ? betweenTenTwenty(n) : ( ten(ten) + " " + oneDigit(one));
}
}
}
//n less than ten
//from 1 to 9
public String oneDigit(int n){
if(n == 1){
return "One";
}else if(n ==2){
return "Two";
}else if(n ==3){
return "Three";
}else if(n ==4){
return "Four";
}else if(n ==5){
return "Five";
}else if(n ==6){
return "Six";
}else if(n ==7){
return "Seven";
}else if(n ==8){
return "Eight";
}else if(n ==9){
return "Nine";
}else{
return "";
}
}
//n is divisible by 10
public String ten(int n){
if(n == 1){
return "Ten";
}else if (n ==2){
return "Twenty";
}else if (n ==3){
return "Thirty";
}else if (n ==4){
return "Forty";
}else if (n ==5){
return "Fifty";
}else if (n ==6){
return "Sixty";
}else if (n ==7){
return "Seventy";
}else if (n ==8){
return "Eighty";
}else if (n ==9){
return "Ninety";
}else{
return "";
}
}
//11-19
public String betweenTenTwenty(int n){
if(n == 11){
return "Eleven";
}else if(n == 12){
return "Twelve";
}else if(n == 13){
return "Thirteen";
}else if(n == 14){
return "Fourteen";
}else if(n == 15){
return "Fifteen";
}else if(n == 16){
return "Sixteen";
}else if(n == 17){
return "Seventeen";
}else if(n == 18){
return "Eighteen";
}else if(n == 19){
return "Nineteen";
}else {
return "";
}
}
}
3. Reference
https://leetcode.com/problems/integer-to-english-words/solution/