Leetcode 338. Counting Bits
来源:互联网 发布:arcgis api for js 编辑:程序博客网 时间:2024/06/03 17:27
题目·: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.
思路:对于前n为的数如果已经统计了,假设结果保存在vector<int> re里面,那第n+1为所对应的应该为re[0~end]依次加一。比如三位的数,每个前面加上个1就可以组成第四位的;第二位的数第三位为0,第一位为1页是组成第四位的。依次相加,直到结果数为num+1为止。
class Solution {public: vector<int> countBits(int num) { vector<int> re; int count1=1; re.push_back(0); while(1){ if(re.size()==num+1) return re; for(int i=0;i<=count1-1;i++) { re.push_back(re[i]+1); if(re.size()==num+1) return re; } count1*=2; } }};
。
阅读全文
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
- python数据分析chapter2-3
- android端上传后台数据 string字符串拼接问题
- leetcode No236. Lowest Common Ancestor of a Binary Tree
- Vue2+VueRouter2+webpack 构建项目实战(三)配置路由,整俩页面先
- MySQL server version for the right syntax to use near ';) T' at line 7
- Leetcode 338. Counting Bits
- [Leetcode] 216. Combination Sum III 解题报告
- UVa442
- JDBC
- ASM AMDU工具使用
- 半路出家, 我的iOS自学之路-2-头文件, 属性, 引用计数, 协议, 类别, 类扩展
- async异步问题(waterfall、parallel、eachSeries、map、whilst)
- cglib动态代理实现
- Contest_5 0614 By lhq