LeetCode 第 338 题 (Counting 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.
借助之前算过的位数来计算,假如ret中保存结果,i的二进制有n位,则ret[i]表示i中bit为1的位数,ret[i >> 1]表示去掉i中最低位的结果,ret[i>>1] + (i&1)即为i的前n-1位为1的数量加上自己最后一位是否为1
class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret;
ret.push_back(0);
for(int i = 1; i <= num; i++)
{
int bits = ret[i >> 1] + (i & 1);
ret.push_back(bits);
}
return ret;
}
};
LeetCode 第 338 题 (Counting Bits)的更多相关文章
- 【Leetcode 338】 Counting Bits
问题描述:给出一个非负整数num,对[0, num]范围内每个数都计算它的二进制表示中1的个数 Example:For num = 5 you should return [0,1,1,2,1,2] ...
- 【leetcode】经典算法题-Counting Bits
题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...
- [Leetcode] 第338题 比特位计数
一.题目描述 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: [0,1,1] 示例 ...
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- Week 8 - 338.Counting Bits & 413. Arithmetic Slices
338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- LeetCode 第 342 题(Power of Four)
LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...
随机推荐
- HttpClient(二)-- 模拟浏览器抓取网页
一.设置请求头消息 User-Agent模拟浏览器 1.当使用第一节的代码 来 访问推酷的时候,会返回给我们如下信息: 网页内容:<!DOCTYPE html> <html> ...
- setTag,getTage复用
radioButtons = new RadioButton[rgMain.getChildCount()]; //遍历RadioGroupfor (int i = 0; i < radioBu ...
- <转>ML 相关算法参考
转自 国内外网站如果你想搜索比较新颖的机器学习资料或是文章,可以到以下网站中搜索,里面不仅包括了机器学习的内容,还有许多其它相关领域内容,如数据科学和云计算等.InfoWord:http://www. ...
- WP8.1学习系列(第二十五章)——控件样式
XAML 框架提供许多自定义应用外观的方法.通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 路线图: 本主题与其他主题有何关联?请参阅: 使用 C# 或 Visua ...
- 【抓包分析】Charles和 夜神模拟器 对安卓应用进行抓包分析
准备工具 : 1 Charles : https://www.charlesproxy.com (收费) 2 夜神模拟器 : https://www.yeshen.com (免费) 2 模拟 ...
- Cross-compilation using Clang
Introduction This document will guide you in choosing the right Clang options for cross-compiling yo ...
- 部署OpenStack问题汇总(一)--使用packstack安装openstack:源问题的处理
在安装的过程中,遇到了源的问题,找不到包的网页: 重新打开 预装源地址,打开epel-openstack-havana.repo 文件,显示如下: # Place this file in yo ...
- MySQL知识小结
MySQL的知识面试中还是经常被问到的,简单的使用似乎无法达到面试官的要求,很多问题会关于Mysql存储引擎,所以这里还是需要系统学习一下Mysql的一些知识,面试过程中游刃有余. MySQL体系结构 ...
- C语言assert()函数用法总结
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...
- 【BZOJ1004】[HNOI2008]Cards Burnside引理
[BZOJ1004][HNOI2008]Cards 题意:把$n$张牌染成$a,b,c$,3种颜色.其中颜色为$a,b,c$的牌的数量分别为$sa,sb,sc$.并且给出$m$个置换,保证这$m$个置 ...