Skip to content

273. Integer to English Words

Description

Convert a non-negative integer num to its English words representation.

 

Example 1:

Input: num = 123
Output: "One Hundred Twenty Three"

Example 2:

Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Example 3:

Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

 

Constraints:

  • 0 <= num <= 231 - 1

Solutions

Solution 1

Thinking

English names group by thousands: Billion, Million, Thousand. Each block has at most three digits and is built from the teens table, the tens table, and Hundred.

\(0\) is Zero. From high to low, convert each nonempty block with \(transfer\) and append the scale word.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution:
    def numberToWords(self, num: int) -> str:
        if num == 0:
            return 'Zero'

        lt20 = [
            '',
            'One',
            'Two',
            'Three',
            'Four',
            'Five',
            'Six',
            'Seven',
            'Eight',
            'Nine',
            'Ten',
            'Eleven',
            'Twelve',
            'Thirteen',
            'Fourteen',
            'Fifteen',
            'Sixteen',
            'Seventeen',
            'Eighteen',
            'Nineteen',
        ]
        tens = [
            '',
            'Ten',
            'Twenty',
            'Thirty',
            'Forty',
            'Fifty',
            'Sixty',
            'Seventy',
            'Eighty',
            'Ninety',
        ]
        thousands = ['Billion', 'Million', 'Thousand', '']

        def transfer(num):
            if num == 0:
                return ''
            if num < 20:
                return lt20[num] + ' '
            if num < 100:
                return tens[num // 10] + ' ' + transfer(num % 10)
            return lt20[num // 100] + ' Hundred ' + transfer(num % 100)

        res = []
        i, j = 1000000000, 0
        while i > 0:
            if num // i != 0:
                res.append(transfer(num // i))
                res.append(thousands[j])
                res.append(' ')
                num %= i
            j += 1
            i //= 1000
        return ''.join(res).strip()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
    private String[] lt20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
        "Seventeen", "Eighteen", "Nineteen"};
    private String[] tens
        = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private String[] thousands = {"Billion", "Million", "Thousand", ""};

    public String numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1000000000, j = 0; i > 0; i /= 1000, ++j) {
            if (num / i == 0) {
                continue;
            }
            sb.append(transfer(num / i)).append(thousands[j]).append(' ');
            num %= i;
        }
        return sb.toString().trim();
    }

    private String transfer(int num) {
        if (num == 0) {
            return "";
        }
        if (num < 20) {
            return lt20[num] + " ";
        }
        if (num < 100) {
            return tens[num / 10] + " " + transfer(num % 10);
        }
        return lt20[num / 100] + " Hundred " + transfer(num % 100);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
    vector<string> lt20 = {
        "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

    vector<string> tens = {
        "", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
        "Sixty", "Seventy", "Eighty", "Ninety"};

    vector<string> thousands = {"Billion", "Million", "Thousand", ""};

    string numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        string res;
        for (int i = 1000000000, j = 0; i > 0; i /= 1000, ++j) {
            int cur = num / i;
            if (cur == 0) {
                continue;
            }
            if (!res.empty()) {
                res += ' ';
            }
            res += transfer(cur);
            if (!thousands[j].empty()) {
                res += ' ';
                res += thousands[j];
            }
            num %= i;
        }
        return res;
    }

private:
    string transfer(int num) {
        if (num == 0) {
            return "";
        }
        if (num < 20) {
            return lt20[num];
        }
        if (num < 100) {
            if (num % 10 == 0) {
                return tens[num / 10];
            }
            return tens[num / 10] + " " + transfer(num % 10);
        }
        if (num % 100 == 0) {
            return lt20[num / 100] + " Hundred";
        }
        return lt20[num / 100] + " Hundred " + transfer(num % 100);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
func numberToWords(num int) string {
    if num == 0 {
        return "Zero"
    }

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

    var transfer func(int) string
    transfer = func(num int) string {
        if num == 0 {
            return ""
        }
        if num < 20 {
            return lt20[num]
        }
        if num < 100 {
            if num%10 == 0 {
                return tens[num/10]
            }
            return tens[num/10] + " " + transfer(num%10)
        }
        if num%100 == 0 {
            return lt20[num/100] + " Hundred"
        }
        return lt20[num/100] + " Hundred " + transfer(num%100)
    }

    res := ""
    for i, j := 1000000000, 0; i > 0; i, j = i/1000, j+1 {
        cur := num / i
        if cur == 0 {
            continue
        }
        if res != "" {
            res += " "
        }
        res += transfer(cur)
        if thousands[j] != "" {
            res += " " + thousands[j]
        }
        num %= i
    }
    return res
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function numberToWords(num: number): string {
    if (num === 0) {
        return 'Zero';
    }

    const lt20 = [
        '',
        'One',
        'Two',
        'Three',
        'Four',
        'Five',
        'Six',
        'Seven',
        'Eight',
        'Nine',
        'Ten',
        'Eleven',
        'Twelve',
        'Thirteen',
        'Fourteen',
        'Fifteen',
        'Sixteen',
        'Seventeen',
        'Eighteen',
        'Nineteen',
    ];

    const tens = [
        '',
        'Ten',
        'Twenty',
        'Thirty',
        'Forty',
        'Fifty',
        'Sixty',
        'Seventy',
        'Eighty',
        'Ninety',
    ];

    const thousands = ['Billion', 'Million', 'Thousand', ''];

    const transfer = (n: number): string => {
        if (n === 0) {
            return '';
        }
        if (n < 20) {
            return lt20[n];
        }
        if (n < 100) {
            if (n % 10 === 0) {
                return tens[Math.floor(n / 10)];
            }
            return tens[Math.floor(n / 10)] + ' ' + transfer(n % 10);
        }
        if (n % 100 === 0) {
            return lt20[Math.floor(n / 100)] + ' Hundred';
        }
        return lt20[Math.floor(n / 100)] + ' Hundred ' + transfer(n % 100);
    };

    let res = '';
    for (let i = 1_000_000_000, j = 0; i > 0; i = Math.floor(i / 1000), ++j) {
        const cur = Math.floor(num / i);
        if (cur === 0) {
            continue;
        }
        if (res !== '') {
            res += ' ';
        }
        res += transfer(cur);
        if (thousands[j] !== '') {
            res += ' ' + thousands[j];
        }
        num %= i;
    }
    return res;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @param {number} num
 * @return {string}
 */
var numberToWords = function (num) {
    if (num === 0) {
        return 'Zero';
    }

    const lt20 = [
        '',
        'One',
        'Two',
        'Three',
        'Four',
        'Five',
        'Six',
        'Seven',
        'Eight',
        'Nine',
        'Ten',
        'Eleven',
        'Twelve',
        'Thirteen',
        'Fourteen',
        'Fifteen',
        'Sixteen',
        'Seventeen',
        'Eighteen',
        'Nineteen',
    ];

    const tens = [
        '',
        'Ten',
        'Twenty',
        'Thirty',
        'Forty',
        'Fifty',
        'Sixty',
        'Seventy',
        'Eighty',
        'Ninety',
    ];

    const thousands = ['Billion', 'Million', 'Thousand', ''];

    const transfer = n => {
        if (n === 0) {
            return '';
        }
        if (n < 20) {
            return lt20[n];
        }
        if (n < 100) {
            if (n % 10 === 0) {
                return tens[Math.floor(n / 10)];
            }
            return tens[Math.floor(n / 10)] + ' ' + transfer(n % 10);
        }
        if (n % 100 === 0) {
            return lt20[Math.floor(n / 100)] + ' Hundred';
        }
        return lt20[Math.floor(n / 100)] + ' Hundred ' + transfer(n % 100);
    };

    let res = '';
    for (let i = 1_000_000_000, j = 0; i > 0; i = Math.floor(i / 1000), ++j) {
        const cur = Math.floor(num / i);
        if (cur === 0) {
            continue;
        }
        if (res !== '') {
            res += ' ';
        }
        res += transfer(cur);
        if (thousands[j] !== '') {
            res += ' ' + thousands[j];
        }
        num %= i;
    }
    return res;
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Solution {
    private readonly string[] lt20 = {
        "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    };

    private readonly string[] tens = {
        "", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
        "Sixty", "Seventy", "Eighty", "Ninety"
    };

    private readonly string[] thousands = { "Billion", "Million", "Thousand", "" };

    public string NumberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }

        string res = "";
        for (int i = 1000000000, j = 0; i > 0; i /= 1000, ++j) {
            int cur = num / i;
            if (cur == 0) {
                continue;
            }
            if (res.Length > 0) {
                res += " ";
            }
            res += Transfer(cur);
            if (thousands[j].Length > 0) {
                res += " " + thousands[j];
            }
            num %= i;
        }
        return res;
    }

    private string Transfer(int num) {
        if (num == 0) {
            return "";
        }
        if (num < 20) {
            return lt20[num];
        }
        if (num < 100) {
            if (num % 10 == 0) {
                return tens[num / 10];
            }
            return tens[num / 10] + " " + Transfer(num % 10);
        }
        if (num % 100 == 0) {
            return lt20[num / 100] + " Hundred";
        }
        return lt20[num / 100] + " Hundred " + Transfer(num % 100);
    }
}

Comments