Leetcode 273 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"
這個題的意思是將數字轉化為英語表達出來,寫的時候要注意單詞拼寫。
程式碼如下:
class Solution {
private final String[] belowTen = new String[]{"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
private final String[] belowTwenty = new String[]{"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
private final String[] belowHundred = new String[]{"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
public String numberToWords(int num) {
if(num == 0){
return "Zero";
}//注意邊界條件
return helper(num);
}
public String helper(int num){
String result = "";
if(num < 10){
result = belowTen[num];
}else if(num < 20){
result = belowTwenty[num - 10];
}else if(num < 100){
result = belowHundred[num / 10] + " " + belowTen[num % 10];
}else if(num < 1000){
result = belowTen[num / 100] + " Hundred " + helper(num % 100);
}else if(num < 1000000){
result = helper(num / 1000) + " Thousand " + helper(num % 1000);
}else if (num < 1000000000){
result = helper(num/1000000) + " Million " + helper(num % 1000000);
}else{
result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
}
return result.trim();//去掉前面和後面的空格。
}
}
相關文章
- LeetCode-Integer to English WordsLeetCode
- set(stopwords.words(‘english‘))
- [English Homwork] Make 10 sentences by using new lesson words
- Leetcode Integer to RomanLeetCode
- leetcode Reverse IntegerLeetCode
- leetcode Roman to IntegerLeetCode
- [LeetCode] Roman to IntegerLeetCode
- How to convert a numeric value or currency to English words using C#C#
- How to describe the main content of the web novel "龍藏" in about 50 English words?AIWeb
- Leetcode Reverse Words in a StringLeetCode
- leetcode Reverse Words in a StringLeetCode
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode 7 Reverse IntegerLeetCode
- LeetCode-Integer ReplacementLeetCode
- LeetCode-Integer BreaksLeetCode
- Leetcode-Roman to IntegerLeetCode
- Leetcode-Integer to RomanLeetCode
- Reverse Integer leetcode javaLeetCodeJava
- Integer to Roman leetcode javaLeetCodeJava
- Roman to Integer leetcode javaLeetCodeJava
- LeetCode-Reverse Words in a StringLeetCode
- Reverse Words in a String leetcode javaLeetCodeJava
- Leetcode 12. Integer to RomanLeetCode
- Leetcode-Problem:Reverse IntegerLeetCode
- leetcode String to Integer (atoi)LeetCode
- Leetcode - String to Integer (atoi)LeetCode
- Leetcode 151 Reverse Words in a StringLeetCode
- LeetCode-Reverse Words in a String IILeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- LeetCode 第 7 題(Reverse Integer)LeetCode
- LeetCode 第 343 題 (Integer Break)LeetCode
- [Swift LeetCode]13. Roman to IntegerSwiftLeetCode
- String to Integer (atoi) leetcode javaLeetCodeJava
- LeetCode之Unique Morse Code Words(Kotlin)LeetCodeKotlin
- LeetCode151:Reverse Words in a StringLeetCode
- Leetcode Substring with Concatenation of All WordsLeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode