记录点滴

Home Archives About
2018-11-28
leetcode

Counting Bits

Counting Bits

Bitwise Operator Version - 108ms

1
2
3
4
5
6
7
8
9
10
class Solution:
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
res = [0]
for i in range(1, num + 1):
res.append(res[i >> 1] + (i & 1))
return res

test code

1
2
In [147]: s = Solution(); t = s.countBits(5); print(t)
[0, 1, 1, 2, 1, 2]

leet code

Share
  • leetcode
  • python3
Newer
Queue Reconstruction by Height
Older
Merge Two Binary Trees

分类

  • bigflow
  • cpp
  • django
  • git
  • json
  • leetcode
  • linux
  • mongodb
  • mysql
  • note
  • opencv
  • python
  • redis

标签

  • 2d-array
  • bigflow
  • binary tree
  • hadoop
  • lambda
  • leetcode
  • python
  • python3

标签云

2d-array bigflow binary tree hadoop lambda leetcode python python3

归档

  • 一月 2020
  • 十二月 2018
  • 十一月 2018
  • 十月 2018
  • 九月 2018
  • 八月 2018
  • 三月 2018
  • 二月 2018
  • 一月 2018
  • 十一月 2017
  • 十月 2017
  • 九月 2017
  • 四月 2017
  • 二月 2017
  • 十一月 2012
  • 十月 2012

最新文章

  • Python requests 超时 异常捕捉
  • mysql distinct count group by
  • (no title)
  • Subarray Sum Equals K
  • Path Sum III
© 2020 Krevy Li
Powered by Hexo
Home Archives About