Skip to content

Commit 7f3bfbd

Browse files
committed
feat(LeetCode): ✨ add largestNumber (lc. 0179)
1 parent 5c46fc9 commit 7f3bfbd

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { largestNumber } from './0179-largestNumber'
2+
3+
it.each(
4+
// prettier-ignore
5+
[
6+
[
7+
[10,2],
8+
"210"
9+
],
10+
[
11+
[3,30,34,5,9],
12+
"9534330"
13+
],
14+
[
15+
[111311, 1113],
16+
"1113111311"
17+
]
18+
]
19+
)('should work %#', (input, output) => {
20+
expect(largestNumber(input)).toBe(output)
21+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* {@link https://leetcode.com/problems/largest-number/ | 179. Largest Number}
3+
*
4+
* Topics: Array | String | Greedy | Sorting
5+
*/
6+
export function largestNumber(nums: number[]): string {
7+
nums.sort((a, b) => {
8+
const [ab, ba] = [String(a) + String(b), String(b) + String(a)]
9+
return +ba - +ab
10+
})
11+
12+
return nums[0] ? nums.join('') : '0'
13+
}

0 commit comments

Comments
 (0)