leetcode 338 Counting Bits C++
来源:互联网 发布:淘口令算淘宝客吗 编辑:程序博客网 时间:2024/05/02 05:01
笨方法
vector<int> countBits(int num) { vector<int> vec; for(int i = 0;i<=num;i++){ int tmp = i; int count = 0; while(tmp != 0){ if((1&tmp) == 1) count++; tmp >>= 1; } vec.push_back(count); } return vec; }
动态规划方法,当前1的个数为最后一位1的个数加上前面部分1的个数。O(n)
vector<int> countBits(int num) { vector<int> vec(num + 1,0); for(int i = 1;i<=num;i++){ vec[i] = vec[i>>1] + (i&1); } return vec; }
0 0
- 【LeetCode-338】Counting Bits(C++)
- leetcode 338 c++. Counting Bits
- [C]LeetCode:Counting Bits
- leetcode:bits:Counting Bits(338)
- 【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
- Counting Bits leetcode 338
- [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 23. Merge k Sorted Lists
- 单元测试
- 如何转载别人的博客
- 图解HTTPS协议加密解密全过程
- 语义分析的一些方法(二)
- leetcode 338 Counting Bits C++
- OpenCV2.4.13环境配置问题
- javascript基础题目
- Ubuntu16.04启动器位置切换
- 静态数组Treap(用随机数维护的二叉搜索树)
- 语义分析的一些方法(三)
- POJ 2189 枚举
- just try
- LA2678 Subsequence 时间优化