MJay

338 Bits 본문

Programming/LeetCode

338 Bits

MJSon 2019. 10. 22. 08:07

338. Counting Bits

Medium

1693120FavoriteShare

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.

꼭 전단계가 아니라 i/2 경우도 생각해야한다. 이건 좀 외워야할거같다.

 

좋은 문제이자 

 

2 bits 니까

 

array를 만들고

 

저장하면 될꺼같다. 

 

i/2 + i % 2 를 더하면 된다. 

 

 

 

'Programming > LeetCode' 카테고리의 다른 글

997  (0) 2019.10.27
750  (0) 2019.10.26
276 - DP - (문제 잘 읽어야하는 이유)  (0) 2019.10.22
198 - DP - House Robber  (0) 2019.10.21
70 - DP - Climbing Stairs  (0) 2019.10.21