LeetCode 338. Counting Bits
来源:互联网 发布:windows 桌面管理 编辑:程序博客网 时间:2024/05/16 23:01
描述
给出一个数,求从0到这个数每个数包含有多少个2进制1.
解决
动态规划的思想。
状态定义:res[i]表示第i个数有多少个1。
状态转移方程: res[i] = res[i / 2] + (i & 1)
class Solution {public: vector<int> countBits(int num) { vector<int> res(num + 1, 0); //res.reserve(num); for (int i = 0; i <= num; ++i){ res[i] = res[i / 2] + (i & 1); // cout << res[i] << endl; } //cout << res.size() << endl; return res; }};
0 0
- [leetcode] 338. Counting Bits
- leetcode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode#338. Counting Bits
- [LeetCode] 338. Counting Bits
- LeetCode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode *** 338. Counting Bits
- (leetcode) 338. Counting Bits
- #leetcode#338. Counting Bits
- LeetCode 338. Counting Bits
- leetcode 338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode-338. Counting Bits
- leetcode-338. Counting Bits
- Leetcode 338. Counting Bits
- LeetCode 338. Counting Bits
- 整数串问题
- hdu4897(树链剖分)
- Java多线程之BlockingQueue深入分析
- Java:应用Observer接口实践Observer模式
- 2015CCPC南阳场 H - Sudoku
- LeetCode 338. Counting Bits
- CentOS tcpflow抓包
- A*B Problem II
- leetcode-python 第五周
- 浏览器打开应用指定的界面
- 单例
- STL map的用法介绍!
- Android—AlertDialog提示信息
- 解决异步获取user session的方法