From dee788529766afab312cff4e13f1a6e24b446922 Mon Sep 17 00:00:00 2001 From: rain84 Date: Wed, 18 Sep 2024 04:22:20 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.0179 --- .../0100-0199/0179.Largest Number/README.md | 26 +++++++++++++++++++ .../0179.Largest Number/README_EN.md | 26 +++++++++++++++++++ .../0100-0199/0179.Largest Number/Solution.js | 8 ++++++ .../0100-0199/0179.Largest Number/Solution.ts | 8 ++++++ 4 files changed, 68 insertions(+) create mode 100644 solution/0100-0199/0179.Largest Number/Solution.js create mode 100644 solution/0100-0199/0179.Largest Number/Solution.ts 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'; +}