add: 最大数

This commit is contained in:
yi-ge 2020-05-05 20:58:22 +08:00
parent 2530c56119
commit a920efef27
3 changed files with 22 additions and 0 deletions

View File

@ -58,6 +58,11 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
- LeetCode 3. 无重复字符的最长子串 https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
- LintCode 384. 最长无重复字符的子串 https://www.lintcode.com/problem/longest-substring-without-repeating-characters/description
- [最大数](src/string/largest-number.js)
- LeetCode 179. 最大数 https://leetcode-cn.com/problems/largest-number/
- LintCode 184. 最大数 https://www.lintcode.com/problem/largest-number/description
## 数组
- [电话号码的字母组合](src/array/letter-combinations-of-a-phone-number.js)

View File

@ -0,0 +1,7 @@
/**
* @param {number[]} nums
* @return {string}
*/
export const largestNumber = function (nums) {
return nums.sort((a, b) => `${b}${a}` - `${a}${b}`).join('').replace(/^0+/, '') || '0'
}

View File

@ -0,0 +1,10 @@
import { largestNumber } from '../../src/string/largest-number'
test('最大数', () => {
expect(largestNumber([10, 2])).toBe('210')
expect(largestNumber([3, 30, 34, 5, 9])).toBe('9534330')
expect(largestNumber([1, 20, 23, 4, 8])).toBe('8423201')
expect(largestNumber([4, 6, 65])).toBe('6654')
expect(largestNumber([])).toBe('0')
expect(largestNumber([0, 0])).toBe('0')
})