Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- programmers
- PYTHON
- 백준
- 더현대서울 맛집
- Docker
- 금융문자분석경진대회
- 캐치카페
- 자연어처리
- ChatGPT
- 파이썬
- 편스토랑
- SW Expert Academy
- 데이콘
- gs25
- 코로나19
- 편스토랑 우승상품
- dacon
- 프로그래머스 파이썬
- Git
- 우분투
- ubuntu
- Kaggle
- AI 경진대회
- 프로그래머스
- leetcode
- Real or Not? NLP with Disaster Tweets
- github
- Baekjoon
- 맥북
- hackerrank
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 179. Largest Number (Python) 본문
728x90
반응형
Given a list of non-negative integers nums, arrange them such that they form the largest number.
Note: The result may be very large, so you need to return a string instead of an integer.
Example 1:
Input: nums = [10,2]
Output: "210"
Example 2:
Input: nums = [3,30,34,5,9]
Output: "9534330"
Example 3:
Input: nums = [1]
Output: "1"
Example 4:
Input: nums = [10]
Output: "10"
Constraints:
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 10^9
첫번째 시도 - 실패 ( Fail )
class Solution:
def largestNumber(self, nums: List[int]) -> str:
if len(nums) == 1:
return str(nums[0])
elif len(nums) == 0:
return ""
else:
temp = []
for num in nums:
temp.append((num, int(str(num)[0])))
temp = sorted(temp, key=lambda x: (-x[1], -x[0]))
answer = [str(t[0]) for t in temp]
return "".join(answer)
두번째 시도 - Solution
class compare(str):
def __lt__(x, y):
return x+y > y+x
class Solution:
def largestNumber(self, nums: List[int]) -> str:
if len(nums) == 1:
return str(nums[0])
elif len(nums) == 0:
return ""
else:
str_nums = [str(num) for num in nums]
answer = sorted(str_nums, key=compare)
if list(set(answer)) == ['0']:
answer = ['0']
return "".join(answer)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 219. Contains Duplicate II (Python) (0) | 2020.10.28 |
---|---|
[leetCode] 217. Contains Duplicate (Python) (0) | 2020.10.26 |
[leetCode] 27. Remove Element (Python) (0) | 2020.10.24 |
[leetCode] 216. Combination Sum III (Python) (0) | 2020.10.23 |
[leetCode] 206. Reverse Linked List (Python) (0) | 2020.10.22 |
Comments