Question

Formatted question description: https://leetcode.ca/all/273.html

 273	Integer to English Words

 Convert a non-negative integer to its english words representation.
 Given input is guaranteed to be less than 2^31 - 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"

Algorithm

The highest can only reach billions, and a group of 3 can only handle 4 groups.

Then we need some function to process three groups of numbers

  • We need to list all English words from 1 to 19 and put them in an array
  • Also list the English words from 20, 30,… to 90 in another array,

Then, a three-digit n, the hundreds digit is expressed as n/100, the last two digits together are expressed as n%100, the tens digit is expressed as n%100/10, and the single digit is expressed as n%10

Then we see if the last two digits are less than 20

  • If it is less than, take the word directly from the array
  • If it is greater than or equal to 20, then take the ten and one digit words from the two arrays respectively

Then deal with the number on the hundreds, and remember to add Hundred.

Call this helper function four times in the main function, then insert “Thousand”, “Million”, “Billion” in the middle to the corresponding position, and finally check whether there are spaces at the end, delete all spaces, and check the input when returning Whether it is 0, if yes, return’Zero’.

Code

Java

import java.util.Arrays;
import java.util.List;

public class Integer_to_English_Words {

    public static void main(String[] args) {
        Integer_to_English_Words out = new Integer_to_English_Words();
        Solution s = out.new Solution();

        System.out.println(s.numberToWords(123));
        System.out.println(s.numberToWords(1234567891));
    }


    class Solution {

        List<String> v1 = Arrays.asList("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
        List<String> v2 = Arrays.asList("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");

        public String numberToWords(int num) {

            String res = convertHundred(num % 1000);
            List<String> unit = Arrays.asList("Thousand", "Million", "Billion", "Trillion");
            for (int i = 0; i < 3; ++i) {
                num /= 1000;
                res = num % 1000 != 0 ? convertHundred(num % 1000) + " " + unit.get(i) + " " + res : res;
            }

            res = res.trim();

            return res.isEmpty() ? "Zero" : res;
        }

        String convertHundred(int num) {

            String res;
            int a = num / 100, b = num % 100, c = num % 10;

            res = b < 20 ?
                    v1.get(b)
                    : v2.get(b / 10) + (c != 0 ? " " + v1.get(c) : "");
            if (a > 0) {
                res = v1.get(a) + " Hundred" + (b != 0 ? " " + res : "");
            }

            return res;
        }
    }
}

All Problems

All Solutions