Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3fbe35

Browse files
authored
feat: add js/ts solutions to lc problem: No.0273 (#3378)
1 parent 6e8cd0d commit e3fbe35

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,58 @@ 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+
325+
#### JavaScript
326+
327+
```js
328+
function numberToWords(num) {
329+
if (num === 0) return 'Zero';
330+
331+
// prettier-ignore
332+
const f = (x) => {
333+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
334+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
335+
let ans = ''
336+
337+
if (x <= 19) ans = dict1[x] ?? ''
338+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
339+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
340+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
341+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
342+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
343+
344+
return ans.trim()
345+
}
346+
347+
return f(num);
348+
}
349+
```
350+
299351
<!-- tabs:end -->
300352
301353
<!-- solution:end -->

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,58 @@ 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+
323+
#### JavaScript
324+
325+
```js
326+
function numberToWords(num) {
327+
if (num === 0) return 'Zero';
328+
329+
// prettier-ignore
330+
const f = (x) => {
331+
const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',]
332+
const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',]
333+
let ans = ''
334+
335+
if (x <= 19) ans = dict1[x] ?? ''
336+
else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}`
337+
else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}`
338+
else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}`
339+
else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}`
340+
else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}`
341+
342+
return ans.trim()
343+
}
344+
345+
return f(num);
346+
}
347+
```
348+
297349
<!-- tabs:end -->
298350
299351
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numberToWords(num) {
2+
if (num === 0) return 'Zero';
3+
4+
// prettier-ignore
5+
const f = (x) => {
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+
}
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)

AltStyle によって変換されたページ (->オリジナル) /