add: 最大数
This commit is contained in:
parent
2530c56119
commit
a920efef27
@ -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)
|
||||
|
7
src/string/largest-number.js
Normal file
7
src/string/largest-number.js
Normal 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'
|
||||
}
|
10
test/string/largest-number.test.js
Normal file
10
test/string/largest-number.test.js
Normal 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')
|
||||
})
|
Loading…
Reference in New Issue
Block a user