33// Solution is mostly from online book solutions
44//
55// Tricks:
6- // - use arrays as data structure for the words
7- // - split at the commas of a long number ( into groups of 3), using a function called "numToStringl00"
8- // - don 't forget to consider negative numbers
6+ // - Use arrays as data structure for the words.
7+ // - Split the number at each comma into groups of 3 digits. Create a function to convert 3-digit numbers to words.
8+ // - Don 't forget to consider negative numbers, and 0.
99
1010public class EnglishInt {
1111
12- private static String [] digits = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" };
13- private static String [] teens = {"Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };
14- private static String [] tens = {"" , "Ten" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" };
15- private static String [] bigs = {"" , "Thousand" , "Million" , "Billion" };
12+ private static String [] first20 = {"" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" };
13+ private static String [] tens = {"" , "Ten" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" };
14+ private static String [] bigs = {"" , "Thousand" , "Million" , "Billion" };
1615
1716 public static String numToString (int num ) {
1817 if (num < 0 ) {
@@ -24,53 +23,28 @@ public static String numToString(int num) {
2423 int bigsIndex = 0 ;
2524 StringBuffer sb = new StringBuffer ();
2625
27- /* We create the string from right to left, inserting in the front. This makes code clean and scalability easier */
26+ // We create the string from right to left, inserting in the front. This makes code clean and scalability easier
2827 while (num > 0 ) {
2928 if (num % 1000 != 0 ) {
3029 sb .insert (0 , numToString100 (num % 1000 ) + bigs [bigsIndex ] + " " );
3130 }
3231 num /= 1000 ;
3332 bigsIndex ++;
3433 }
35- return sb .toString ();
34+ return sb .toString (). trim () ;
3635 }
3736
38- public static String numToString100 (int num ) {
39- if (num < 0 ) {
40- return "Negative " + numToString (-1 * num );
41- } else if (num == 0 ) {
42- return "Zero" ;
43- } else if (num > 999 ) {
44- return null ;
45- }
46-
47- StringBuffer sb = new StringBuffer ();
48-
49- /* Hundreds */
50- if (num >= 100 ) {
51- int digit = num / 100 ;
52- sb .append (digits [digit ] + " Hundred " );
53- num = num % 100 ; // now we have a 2-digit number
54- }
55-
56- /* Tens */
57- int tensDigit = num / 10 ;
58- int singleDigit = num % 10 ;
59-
60- if (num >= 10 && num <= 19 ) {
61- sb .append (teens [singleDigit ] + " " );
62- return sb .toString ();
63- } else if (num >= 20 ) {
64- sb .append (tens [tensDigit ] + " " );
65- }
66-
67- /* Single Digit */
68- if (singleDigit != 0 ) {
69- sb .append (digits [singleDigit ] + " " );
70- }
71-
72- return sb .toString ();
37+ private static String numToString100 (int num ) { // assumes 0 < num < 1000
38+ if (num == 0 )
39+ return "" ;
40+ else if (num < 20 )
41+ return first20 [num ] + " " ;
42+ else if (num < 100 )
43+ return tens [num / 10 ] + " " + numToString100 (num % 10 );
44+ else
45+ return first20 [num / 100 ] + " Hundred " + numToString100 (num % 100 );
7346 }
7447}
7548
76- // Time Complexity: O(1)
49+ // Time Complexity: O(1)
50+ // Space Complexity: O(1)
0 commit comments