1. 程式人生 > >leetcode 338 Counting Bits python3 多種(最簡)解法,尋求數學規律

leetcode 338 Counting Bits python3 多種(最簡)解法,尋求數學規律

”’
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:
For num = 5 you should return [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.
Credits:
Special thanks to @ syedee for adding this problem and creating all test cases

”’

class Solution:
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        # 一次遍歷
        # return [bin(i)[2:].count('1') for i in range(num+1)]



        # method two   更加高效的解法,少呼叫內建函式,挖掘資料中的規律


#    0000    0
# -------------
#    0001    1
# -------------
# 0010 1 # 0011 2 # ------------- # 0100 1 # 0101 2 # 0110 2 # 0111 3 # ------------- # 1000 1 # 1001 2 # 1010 2 # 1011 3 # 1100 2 # 1101 3 # 1110 3 # 1111 4 # ans = [0] # while len(ans) < num + 1: # ans += [1 + x for x in ans]
# return ans[:num+1] # Approach #3 利用 i & i-1 進一步挖掘位元位計數的規律。 每個數i所對應的位元位計數的答案是 i & i-1 計算結果的數字,所對應的位元位計數結果加一。 # bin '1' i&(i-1) # 0000 0 # ----------------------- # 0001 1 0000 # ----------------------- # 0010 1 0000 # 0011 2 0010 # ----------------------- # 0100 1 0000 # 0101 2 0100 # 0110 2 0100 # 0111 3 0110 # ----------------------- # 1000 1 0000 # 1001 2 1000 # 1010 2 1000 # 1011 3 1010 # 1100 2 1000 # 1101 3 1100 # 1110 3 1100 # 1111 4 1110 res = [0] for i in range(1,num+1): res += [res[ i & i-1] + 1] return res