-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0273.go
116 lines (106 loc) · 2.99 KB
/
0273.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Source: https://leetcode.com/problems/integer-to-english-words
// Title: Integer to English Words
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 <= 2^31 - 1
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var names = map[int]string{
0: "Zero",
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
11: "Eleven",
12: "Twelve",
13: "Thirteen",
14: "Fourteen",
15: "Fifteen",
16: "Sixteen",
17: "Seventeen",
18: "Eighteen",
19: "Nineteen",
20: "Twenty",
30: "Thirty",
40: "Forty",
50: "Fifty",
60: "Sixty",
70: "Seventy",
80: "Eighty",
90: "Ninety",
100: "Hundred",
1_000: "Thousand",
1_000_000: "Million",
1_000_000_000: "Billion",
}
func numberToWords(num int) string {
if num < 20 {
return names[num]
}
if num < 100 {
ones := num % 10
tens := num - ones
if ones == 0 {
return names[tens]
}
return names[tens] + " " + names[ones]
}
if num < 1_000 {
remainder := num % 100
quotient := num / 100
if remainder == 0 {
return names[quotient] + " " + names[100]
}
return names[quotient] + " " + names[100] + " " + numberToWords(remainder)
}
if num < 1_000_000 {
remainder := num % 1_000
quotient := num / 1_000
if remainder == 0 {
return numberToWords(quotient) + " " + names[1_000]
}
return numberToWords(quotient) + " " + names[1_000] + " " + numberToWords(remainder)
}
if num < 1_000_000_000 {
remainder := num % 1_000_000
quotient := num / 1_000_000
if remainder == 0 {
return numberToWords(quotient) + " " + names[1_000_000]
}
return numberToWords(quotient) + " " + names[1_000_000] + " " + numberToWords(remainder)
}
remainder := num % 1_000_000_000
quotient := num / 1_000_000_000
if remainder == 0 {
return numberToWords(quotient) + " " + names[1_000_000_000]
}
return numberToWords(quotient) + " " + names[1_000_000_000] + " " + numberToWords(remainder)
}