Re: [閒聊] 每日leetcode
179. Largest Number
## 思路
sorting
寫個compare function比較數字字串 (A+B , B+A)
然後有個corner case [0, 0, 0] -> '0'
## Code
```python
from functools import cmp_to_key
class Solution:
def largestNumber(self, nums: List[int]) -> str:
nums = list(map(str, nums))
def compare(a, b):
if a + b > b + a:
return 1
if a + b < b + a:
return -1
return 0
nums.sort(key=cmp_to_key(compare), reverse=True)
if nums[0] == '0':
return '0'
return ''.join(nums)
```
--
https://i.imgur.com/kyBhy6o.jpeg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.48 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726642178.A.20D.html
推
09/18 14:50,
1年前
, 1F
09/18 14:50, 1F
→
09/18 14:50,
1年前
, 2F
09/18 14:50, 2F
→
09/18 14:51,
1年前
, 3F
09/18 14:51, 3F
推
09/18 14:54,
1年前
, 4F
09/18 14:54, 4F
→
09/18 14:59,
1年前
, 5F
09/18 14:59, 5F
→
09/18 15:00,
1年前
, 6F
09/18 15:00, 6F
→
09/18 15:01,
1年前
, 7F
09/18 15:01, 7F
→
09/18 15:05,
1年前
, 8F
09/18 15:05, 8F
討論串 (同標題文章)
完整討論串 (本文為第 875 之 1548 篇):