leetcode刷题,总结,记录,备忘 338
来源:互联网 发布:人工智能加医疗概念股 编辑:程序博客网 时间:2024/06/13 01:45
leetcode338Counting Bits
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.
Example:
For num = 5
you should return [0,1,1,2,1,2]
.
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
class Solution {public: vector<int> countBits(int num) { vector<int> result; result.push_back(0); if (num == 0) { return result; } result.push_back(1); if (num == 1) { return result; } int root = 1; for (int i = 2; i <= num; ++i) { if (i == 2*root) { result.push_back(1); root *= 2; } else { result.push_back(result[i % root] + 1); } } return result; }};
0 0
- leetcode刷题,总结,记录,备忘 338
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- sqlite3 api&cli
- strace命令的一个例子
- Stanford 机器学习 Week8 作业:K-means Clustering and Principal Component Analysis
- 213. House Robber II
- Ubuntu12.04 安装adb
- leetcode刷题,总结,记录,备忘 338
- 第四周项目5.3—编写递归函数求两个数的最大公约数
- 重要的源码地址
- 删数问题--贪心算法+string类的一些应用
- 图灵机功能总结
- php:字符串的合并与分割
- android 中theme.xml与style.xml的区别
- 关于ContentType-charset对于浏览器编码的作用
- C++11智能指针——weak_ptr