leetcode 338 Counting Bits python3 多种(最简)解法,寻求数学规律

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。

‘’’
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
        
        
        
        
        
    
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值