Skip to content

Commit 714aa26

Browse files
committed
feat: add ts solution to lc problem: No.0273
1 parent 6e8cd0d commit 714aa26

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solution/0200-0299/0273.Integer to English Words/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,32 @@ public class Solution {
296296
}
297297
```
298298

299+
#### TypeScript
300+
301+
```ts
302+
function numberToWords(num: number): string {
303+
if (num === 0) return 'Zero'
304+
305+
// prettier-ignore
306+
const f = (x: number): string => {
307+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
308+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
309+
let ans = ''
310+
311+
if (x <= 19) ans = dict1[x] ?? ''
312+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
313+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
314+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
315+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
316+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
317+
318+
return ans.trim()
319+
}
320+
321+
return f(num)
322+
}
323+
```
324+
299325
<!-- tabs:end -->
300326

301327
<!-- solution:end -->

solution/0200-0299/0273.Integer to English Words/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,32 @@ public class Solution {
294294
}
295295
```
296296

297+
#### TypeScript
298+
299+
```ts
300+
function numberToWords(num: number): string {
301+
if (num === 0) return 'Zero'
302+
303+
// prettier-ignore
304+
const f = (x: number): string => {
305+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
306+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
307+
let ans = ''
308+
309+
if (x <= 19) ans = dict1[x] ?? ''
310+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
311+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
312+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
313+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
314+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
315+
316+
return ans.trim()
317+
}
318+
319+
return f(num)
320+
}
321+
```
322+
297323
<!-- tabs:end -->
298324

299325
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numberToWords(num: number): string {
2+
if (num === 0) return 'Zero';
3+
4+
// prettier-ignore
5+
const f = (x: number): string => {
6+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
7+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
8+
let ans = ''
9+
10+
if (x <= 19) ans = dict1[x] ?? ''
11+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
12+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
13+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
14+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
15+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
16+
17+
return ans.trim()
18+
}
19+
20+
return f(num);
21+
}

0 commit comments

Comments
 (0)