Very Easy To understand using HashMap.


  • 0
    S

    Each time I will get the last three digit from the existing num % 1000, which I denote here as thousands. Then with the three digit number we just get, I divide it further into last two(unit digit and tens digit) and last third(hundreds).

    With the last two digit, we just need to take care of them by checking if it is between 1 to 19. For numbers in this range or it can be divided by 10, we get the corresponding string directly from the Hashmap. For other numbers which are greater or equal than 20 and with unit digit. We divide it further into two separate digits and get the tens and unit digits from HashMap.

    For the last third digit, we add string number of this digit and followed by "Hundred" if this digit is greater than zero.

    Each time after the num divided by 1000 can get remains after mod next 1000, it means that the higher three digit numbers exist, then we will add "thousand", "million", "billion" accordingly.

    For example:

    num = 1123456
    thousands = 1123456 % 1000 = 456
    tens = 456 % 100 = 56
    first_digit = 5 "Fifty"
    last_digit = 6 "Six"
    hundreds = 456/100 = 4 "Four Hundred"

    We'll make sure "Thousand" will be followed next time if higher three digits are not all 0s
    1123456 / 1000 %1000 = 123 != 0 "Thousand"

    public class Solution {
        public String numberToWords(int num) {
            if (num == 0) return "Zero";
            HashMap<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "One");
            map.put(2, "Two");
            map.put(3, "Three");
            map.put(4, "Four");
            map.put(5, "Five");
            map.put(6, "Six");
            map.put(7, "Seven");
            map.put(8, "Eight");
            map.put(9, "Nine");
            map.put(10, "Ten");
            map.put(11, "Eleven");
            map.put(12, "Twelve");
            map.put(13, "Thirteen");
            map.put(14, "Fourteen");
            map.put(15, "Fifteen");
            map.put(16, "Sixteen");
            map.put(17, "Seventeen");
            map.put(18, "Eighteen");
            map.put(19, "Nineteen");
            map.put(20, "Twenty");
            map.put(30, "Thirty");
            map.put(40, "Forty");
            map.put(50, "Fifty");
            map.put(60, "Sixty");
            map.put(70, "Seventy");
            map.put(80, "Eighty");
            map.put(90, "Ninety");
            map.put(100, "Hundred");
            map.put(1000, "Thousand");
            map.put(1000000, "Million");
            map.put(1000000000, "Billion");
            String ret = "";
            int base = 1000;
            while (num > 0) {
                int thousands = num % 1000;
                if (thousands > 0) {
                    int tens = thousands % 100;
                    int hundreds = thousands / 100;
                    if ((0 < tens && tens < 20) || (tens > 19 && tens % 10 == 0)) {
                        ret = map.get(tens) + " " + ret;
                    } else {
                        int second_digit = tens % 10;
                        int first_digit = (tens / 10) * 10;
                        if (second_digit > 0) ret = map.get(second_digit) + " " + ret;
                        if (first_digit > 0) ret = map.get(first_digit) + " " + ret;
                    }
                    if (hundreds > 0) {
                        ret = map.get(hundreds) + " Hundred " + ret;
                    }
                }
                // Make sure thousand, million, billion will be outputted/Hidden properly.
                if ((num / 1000) % 1000 != 0) {
                    ret = map.get(base) + " " + ret;
                }
                base = base * 1000;
                num = num / 1000;
            }
            return ret.trim();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.