273 Integer to English Words
273 Integer to English Words string.trim()is a built-in function that eliminates leading and trailing spaces. https://www.geeksforgeeks.org/java-string-trim-method-example/ Input: 1, 234, 567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 这道题就是先把 567 先转化掉, 再 转化 234, 接个 Thousand, 按照 i 的个数 增常,然后再 转化 1, 接个 Million Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12 , 345 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" 1,000,000,000 one billion 1,000,000 one million 1,000 one thousand class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0){ return "Zero"; } int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0){ words = helper(num % 1000) +THOUSANDS[i] + " " + words; } num = num /1000; i++; } return words.trim(); } private String helper(int num) { if (num == 0){ return ""; }else if (num < 20){ return LESS_THAN_20[num] + " "; }else if (num < 100){ return TENS[num / 10] + " " + helper(num % 10); }else{ return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100); } } } // 2. number = 123 123 % 1000 = 123 helper(123 % 1000) + thousand[0] + helper(123) 123/100 = 1 (one from less than 20 [1] ) hundred + helper(123 % 100) helper(123 % 100 = 23) tens(23/20= 2) twenty + helper(23 % 10) 1. num = 5000 5000 % 1000 = 0 num = num / 1000 = 5000/1000 = 5 i = 1 num = 5 > 0 5 % 1000 = 5 helper(5 % 1000) + thousand[1] + "" + words = five thousand
1,234,567
One (million)
234(thousand)
567()
I = 0
Words = “”
Words = helper(1,234,567 % 1000) + thousand[I = 1] + “” + words
helper(1,234,567 % 1000 = 567 ) + thousand[I = 0] + “” + words
Num = num /1000 = 1, 234
I++.
I = 1
=================
1,234,567 > 0
1,234,567 = 1234 * 1000 + 567
1,234,567 % 1000 = 567
helper(567) + thousand(I = 0 ) + “” + words
===========================
helper(567) = five hundred sixty seven
helper(567)
567 > 20
567 > 100
Less than twenty(567 / 100) + “hundred” + helper(567 % 100)
Less than twenty(567/ 100) = “five”
helper(567 % 100) = helper(67) = sixty seven
helper(67)
67 > 20
67 > 100
tens(67 /100= 6) + “” + helper(67 % 10 = 7)
tens(6) = sixty
helper(7) =
7 < 20
= less than twenty (7) + “” = “seven”
===========================
thousand[I] = thousand
Words = five hundred sixty-seven
Words = helper(1,234 % 1000 = 234 ) + thousand[I = 1] + “” + words
Num = num / 1000 = 1
I++.
I = 2
====================
helper(1,234 % 1000 = 234 )
helper(234) =
Same as above for getting 567 , we got two hundred thirty four
thousand[I = 1] = thousand
So words = two hundred thirty four + thousand
=====
Num = 1 > 0
Num % 1000 = 1 != 0
Words = two hundred thirty four + thousand + five hundred sixty seven
Words = helper(num % 1000 = 1) + thousand (2) + “” + words
thousand (2) = million
Words = one million + two hundred thirty four + thousand + five hundred sixty seven
num = num / 1000 = 1 / 1000 = 0
Return Words = one million + two hundred thirty four + thousand + five hundred sixty seven
先用recursive方法,followup是iterative的
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"
posted on 2018-08-09 18:56 猪猪🐷 阅读(130) 评论(0) 收藏 举报
浙公网安备 33010602011771号