일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 읽기
- leetcode
- 개발자
- 화상영어
- 3줄정리
- English
- FIT XR
- 리얼 클래스
- 스탭퍼
- 링피트
- 영어공부
- 10분
- 괜찮음
- 월간
- 파비최
- 잡생각
- 영어원서읽기
- 만화도
- 뭐든
- 30분
- 미드시청
- 운동
- Writing
- 사이드
- 쓰릴오브파이트
- Problem Solving
- realclass
- 매일
- 프로젝트
- Daily Challenge
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.10.31 Today's Challenge 본문
https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/
Sort Integers by The Number of 1 Bits - LeetCode
Can you solve this real interview question? Sort Integers by The Number of 1 Bits - You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integ
leetcode.com
오랫만에 직접 품.
좀 더 간단한 방법이 있을지도..
class Solution:
def sortByBits(self, arr: List[int]) -> List[int]:
d = {}
res = []
for v in arr:
k = bin(v).count('1')
if not d.get(k):
d[k] = [v]
continue
d[k].append(v)
d = sorted(d.items())
for dd in d:
res.extend(sorted(dd[1]))
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.11.02 Today's Challenge (0) | 2023.11.02 |
---|---|
2023.11.01 Today's Challenge (0) | 2023.11.01 |
2023.10.30 Today's Challenge (0) | 2023.10.30 |
2023.10.29 Today's Challenge (0) | 2023.10.29 |
2023.10.28 Today's Challenge (0) | 2023.10.28 |
Comments