diff --git a/solution/0100-0199/0179.Largest Number/README.md b/solution/0100-0199/0179.Largest Number/README.md index e99edd8a8d578..40b90227aad02 100644 --- a/solution/0100-0199/0179.Largest Number/README.md +++ b/solution/0100-0199/0179.Largest Number/README.md @@ -183,6 +183,32 @@ public class Solution { } ``` +#### TypeScript + +```ts +function largestNumber(nums: number[]): string { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +} +``` + +#### JavaScript + +```js +function largestNumber(nums) { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +} +``` + diff --git a/solution/0100-0199/0179.Largest Number/README_EN.md b/solution/0100-0199/0179.Largest Number/README_EN.md index f8a1a3697882b..d8c0dff8f01ed 100644 --- a/solution/0100-0199/0179.Largest Number/README_EN.md +++ b/solution/0100-0199/0179.Largest Number/README_EN.md @@ -180,6 +180,32 @@ public class Solution { } ``` +#### TypeScript + +```ts +function largestNumber(nums: number[]): string { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +} +``` + +#### JavaScript + +```js +function largestNumber(nums) { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +} +``` + diff --git a/solution/0100-0199/0179.Largest Number/Solution.js b/solution/0100-0199/0179.Largest Number/Solution.js new file mode 100644 index 0000000000000..9acfe19b38c93 --- /dev/null +++ b/solution/0100-0199/0179.Largest Number/Solution.js @@ -0,0 +1,8 @@ +function largestNumber(nums) { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +} diff --git a/solution/0100-0199/0179.Largest Number/Solution.ts b/solution/0100-0199/0179.Largest Number/Solution.ts new file mode 100644 index 0000000000000..996e95f369ea4 --- /dev/null +++ b/solution/0100-0199/0179.Largest Number/Solution.ts @@ -0,0 +1,8 @@ +function largestNumber(nums: number[]): string { + nums.sort((a, b) => { + const [ab, ba] = [String(a) + String(b), String(b) + String(a)]; + return +ba - +ab; + }); + + return nums[0] ? nums.join('') : '0'; +}