Skip to content

Commit 66fc197

Browse files
committed
feat: solve No.273
1 parent 0f3914b commit 66fc197

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# 273. Integer to English Words
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Math, String, Recursion.
5+
- Similar Questions: Integer to Roman.
6+
7+
## Problem
8+
9+
Convert a non-negative integer `num` to its English words representation.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: num = 123
16+
Output: "One Hundred Twenty Three"
17+
```
18+
19+
Example 2:
20+
21+
```
22+
Input: num = 12345
23+
Output: "Twelve Thousand Three Hundred Forty Five"
24+
```
25+
26+
Example 3:
27+
28+
```
29+
Input: num = 1234567
30+
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
31+
```
32+
33+
 
34+
**Constraints:**
35+
36+
37+
38+
- `0 <= num <= 231 - 1`
39+
40+
41+
42+
## Solution
43+
44+
```javascript
45+
/**
46+
* @param {number} num
47+
* @return {string}
48+
*/
49+
var numberToWords = function(num) {
50+
if (num === 0) return 'Zero';
51+
var words1 = [
52+
'One',
53+
'Two',
54+
'Three',
55+
'Four',
56+
"Five",
57+
"Six",
58+
'Seven',
59+
'Eight',
60+
'Nine',
61+
];
62+
var words2 = [
63+
'Ten',
64+
'Eleven',
65+
'Twelve',
66+
'Thirteen',
67+
'Fourteen',
68+
'Fifteen',
69+
'Sixteen',
70+
'Seventeen',
71+
'Eighteen',
72+
'Nineteen',
73+
];
74+
var words3 = [
75+
'Ten',
76+
'Twenty',
77+
'Thirty',
78+
'Forty',
79+
'Fifty',
80+
'Sixty',
81+
'Seventy',
82+
'Eighty',
83+
'Ninety',
84+
];
85+
var getStr = function(number) {
86+
var res = [];
87+
var num1 = Math.floor(number / 100);
88+
if (num1 > 0) {
89+
res.push(`${words1[num1 - 1]} Hundred`);
90+
}
91+
number %= 100;
92+
if (number >= 10 && number <= 19) {
93+
res.push(words2[number - 10]);
94+
} else {
95+
var num2 = Math.floor(number / 10);
96+
if (num2 > 0) {
97+
res.push(words3[num2 - 1]);
98+
}
99+
number %= 10;
100+
if (number > 0) {
101+
res.push(words1[number - 1]);
102+
}
103+
}
104+
return res.join(' ');
105+
};
106+
var res = [];
107+
[
108+
[Math.pow(10, 9), 'Billion'],
109+
[Math.pow(10, 6), 'Million'],
110+
[Math.pow(10, 3), 'Thousand'],
111+
].forEach(item => {
112+
var num1 = Math.floor(num / item[0]);
113+
if (num1 > 0) {
114+
res.push(`${getStr(num1)} ${item[1]}`);
115+
}
116+
num %= item[0];
117+
});
118+
if (num > 0) {
119+
res.push(getStr(num));
120+
}
121+
return res.join(' ');
122+
};
123+
```
124+
125+
**Explain:**
126+
127+
nope.
128+
129+
**Complexity:**
130+
131+
* Time complexity : O(log(n)).
132+
* Space complexity : O(log(n)).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy