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
- 맥북
- 더현대서울 맛집
- 프로그래머스 파이썬
- 데이콘
- hackerrank
- Real or Not? NLP with Disaster Tweets
- Kaggle
- github
- 코로나19
- programmers
- ubuntu
- 프로그래머스
- dacon
- gs25
- SW Expert Academy
- Git
- ChatGPT
- leetcode
- 자연어처리
- 금융문자분석경진대회
- 파이썬
- 우분투
- 편스토랑
- Docker
- 캐치카페
- 백준
- Baekjoon
- AI 경진대회
- 편스토랑 우승상품
- PYTHON
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 338. Counting Bits (Pyhton) 본문
728x90
반응형
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example 1:
Input: 2
Output: [0,1,1]
Example 2:
Input: 5
Output: [0,1,1,2,1,2]
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
Solution
class Solution:
def countBits(self, num: int) -> List[int]:
results = [0]
for i in range(1, num+1):
results.append(results[i & (i-1)] + 1)
return results
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 148. Sort List (Python) (0) | 2020.11.29 |
---|---|
[leetCode] 468. Validate IP Address (Python) (0) | 2020.11.22 |
[leetCode] 313. Super Ugly Number (Python) (0) | 2020.11.20 |
[leetCode] 165. Compare Version Numbers (Python) (0) | 2020.11.19 |
[leetCode] 77. Combinations (Python) (0) | 2020.11.18 |
Comments