essentially, this is exactly the same with Integer to Roman or Integer to any other calculate method.

I hate questions like this, let’s hope I won’t met problems like this in a real interview.

class Solution {
    String[] less20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    String[] thousands = {"", "Thousand", "Million", "Billion"};

    public String numberToWords(int num) {
        if (num == 0) return "Zero";
        String res = "";
        int i = 0;
        while (num > 0) {
            if (num % 1000 != 0) { // if num % 1000 == 0, then next time, num will be 1 and we will execute this if. if we run case 1000, which indicates it is crrect
                res = helper(num % 1000) + thousands[i] + " " + res; 
            }
            num /= 1000;
            i++;
        }
        return res.trim();
    }
    public String helper(int num) { // everytime num is a 3 digits number
        if (num == 0) return "";
        if (num < 20) { // num less than 20
            return less20[num % 20] + " ";
        } else if (num < 100) { //nums 20~99
            return tens[num / 10] + " " + helper(num % 10);
        } else { //>=100
            return less20[num / 100] + " Hundred " + helper(num % 100);
        }
    }
}

from the code above we can see that we divide number every three digits.
and we using helper function process numbers less that 1000. and we will start from num<20, num<100, nums>=100.

the idea of this code couldn’t be more clear.