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();//去掉前面和後面的空格。
}
}
相關文章
- set(stopwords.words(‘english‘))
- How to describe the main content of the web novel "龍藏" in about 50 English words?AIWeb
- Leetcode 151 Reverse Words in a StringLeetCode
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode 7 Reverse IntegerLeetCode
- Leetcode 30 Substring with Concatenation of All WordsLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- Leetcode 12. Integer to RomanLeetCode
- LeetCode之Unique Morse Code Words(Kotlin)LeetCodeKotlin
- [LeetCode] 884. Uncommon Words from Two SentencesLeetCode
- LeetCode Integer to Roman(012)解法總結LeetCode
- LeetCode Roman to Integer(013)解法總結LeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- [LeetCode] Short Encoding of Words 單詞集的短編碼LeetCodeEncoding
- Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)LeetCode字串
- English 4
- Business talking in English
- bag-of-words
- The Future of the English [Supplementary Exercises]
- OKR-Periods of WordsOKR
- Substring with Concatenation of All Words
- English Metric Units 介紹
- NEW CONCEPT ENGLISH 51 - 60
- Integer比較
- 【Using English】28 - Security with HTTPS and SSLHTTP
- The English names of various berries All In One
- Learn English 10 times faster with these tipsAST
- Window Application has "update" key wordsAPP
- *692. Top K Frequent Words
- Business English 商務一點通
- Linguistics-English-Psychology-Minds: WholePurposeOfLife + Success +NGUI
- 【躍遷之路】【514天】刻意練習系列273(2018.07.04)