diff --git a/solution/0200-0299/0273.Integer to English Words/README.md b/solution/0200-0299/0273.Integer to English Words/README.md index 54a28c8948824..a7fce1803bf4e 100644 --- a/solution/0200-0299/0273.Integer to English Words/README.md +++ b/solution/0200-0299/0273.Integer to English Words/README.md @@ -296,6 +296,58 @@ public class Solution { } ``` +#### TypeScript + +```ts +function numberToWords(num: number): string { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + +#### JavaScript + +```js +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/README_EN.md b/solution/0200-0299/0273.Integer to English Words/README_EN.md index 51615ba7e4a7a..3260ff5261c97 100644 --- a/solution/0200-0299/0273.Integer to English Words/README_EN.md +++ b/solution/0200-0299/0273.Integer to English Words/README_EN.md @@ -294,6 +294,58 @@ public class Solution { } ``` +#### TypeScript + +```ts +function numberToWords(num: number): string { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + +#### JavaScript + +```js +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} +``` + diff --git a/solution/0200-0299/0273.Integer to English Words/Solution.js b/solution/0200-0299/0273.Integer to English Words/Solution.js new file mode 100644 index 0000000000000..4abc586bf2dee --- /dev/null +++ b/solution/0200-0299/0273.Integer to English Words/Solution.js @@ -0,0 +1,21 @@ +function numberToWords(num) { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x) => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +} diff --git a/solution/0200-0299/0273.Integer to English Words/Solution.ts b/solution/0200-0299/0273.Integer to English Words/Solution.ts new file mode 100644 index 0000000000000..32c8e3b3a24a3 --- /dev/null +++ b/solution/0200-0299/0273.Integer to English Words/Solution.ts @@ -0,0 +1,21 @@ +function numberToWords(num: number): string { + if (num === 0) return 'Zero'; + + // prettier-ignore + const f = (x: number): string => { + const dict1 = ['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen',] + const dict2 = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety',] + let ans = '' + + if (x <= 19) ans = dict1[x] ?? '' + else if (x < 100) ans = `${dict2[Math.floor(x / 10)]} ${f(x % 10)}` + else if (x < 10 ** 3) ans = `${dict1[Math.floor(x / 100)]} Hundred ${f(x % 100)}` + else if (x < 10 ** 6) ans = `${f(Math.floor(x / 10 ** 3))} Thousand ${f(x % 10 ** 3)}` + else if (x < 10 ** 9) ans = `${f(Math.floor(x / 10 ** 6))} Million ${f(x % 10 ** 6)}` + else ans = `${f(Math.floor(x / 10 ** 9))} Billion ${f(x % 10 ** 9)}` + + return ans.trim() + } + + return f(num); +}