LeetCode "Counting Bits"
2016-03-21 12:15
369 查看
Neat DP problem to go.
If you don't like log(), you can use lookup table.
And, this is an amazing solution: https://leetcode.com/discuss/93501/simple-and-4-lines
from math import * class Solution(object): def countBits(self, num): ret = [0] for i in range(1, num + 1): if i & 1: # odd ret += [ret[-1] + 1] else: prev = i - 1 trailing1s = (i | prev) - i cnt = int(math.log(trailing1s + 1, 2)) ret += [ret[-1] + 1 - cnt] return ret
If you don't like log(), you can use lookup table.
And, this is an amazing solution: https://leetcode.com/discuss/93501/simple-and-4-lines
相关文章推荐
- 栈在java中的实现
- mysql锁-乐观锁
- Docker历险记(一)
- js 跨域访问
- PHP进阶篇:取得当前的日期
- Java并发编程(三)——synchronized
- Span 介绍及使用(二)
- python的unicode处理关系
- 《深入浅出数据分析》笔记二
- Java并发编程(二)——Thread.currentThread()研究
- JSF的ManagedBean与Spring Bean的比较与集成
- Span 介绍及使用(一)
- 使用appium进行ios测试,启动inspector时遇到的问题(一)
- PHP进阶:取得当前的Unix时间戳
- 互联网迎来“独立日”?
- 非阻塞式线程安全列表
- 敏捷开发之Scrum扫盲篇
- 人机大战之AlphaGo的硬件配置和算法研究
- UITableView的段落设置 — 段头段尾高度
- [Leetcode][数论]Add Digits