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.
分析:
这道题完全没看别人的思路,自己想出来的第一道动态规划题目,开心<( ̄︶ ̄)>
当n为奇数时,则n-1为偶数,由二进制可知,n-1的二进制最后一位必定是0
而n的二进制就是在n-1的二进制最后一位加1,并未影响n-1的前面的情况,只把最后一位0,变为了1,所以当n=奇数时,dp[n]=dp[n-1]+1;
举个栗子:6=1*(2^2)+1*(2^1)+0*(2^0),6的二进制是110.
7=1*(2^2)+1*(2^1)+1*(2^0),7的二进制是111.
当n为偶数时,先举个栗子吧,
6=1*(2^2)+1*(2^1)+0*(2^0),6的二进制是110.
n/2就是把每一项都除以2,但是你会发现每一项的系数是不变的,
6/2=3=1*(2^1)+1*(2^0)+0(2^0)
所以当n=偶数时,dp[n]=dp[n/2].
class Solution {
public:
vector<int> countBits(int num) {
vector<int> dp(num+1,0);
for(int i=1;i<num+1;i++){
if(i%2==0)
dp[i]=dp[i/2];
else
dp[i]=dp[i-1]+1;
}
return dp;
}
};
338. Counting Bits(动态规划)的更多相关文章
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 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】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 338. Counting Bits题目详解
题目详情 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- 338. Counting Bits
https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- Leetcode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- Leet Code OJ 338. Counting Bits [Difficulty: Medium]
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
随机推荐
- Android横竖屏切换不重新调用onCreate()
再次感叹Android的碎片化!!!! 设置AndroidManifest.xml中Activity的android:configChanges=”keyboardHidden|orientation ...
- XAML实例教程系列 - 类型转换器(Type Converter)七
XAML实例教程系列 - 类型转换器(Type Converter) 分类: Windows 8 Silverlight2012-06-25 13:40 961人阅读 评论(0) 收藏 举报 butt ...
- HTTP缓存控制 总结
一.HTTP响应头.请求头中与缓存控制的相关字段 二.一个页面访问缓存的流程 三.三种刷新的实际操作 四.如何设置缓存 一.HTTP响应头.请求头中与缓存控制的相关字段 浏览器向服务器发起请求后,服务 ...
- 使用js模拟ecshop元素挪移
<!DOCTYPE html><html><head> <title>移动</title> <script src="jqu ...
- php中strlen,mb_strlen,count之区别
转自 http://www.cnblogs.com/mo-beifeng/archive/2011/08/09/2133039.html 这不是一个固定的数字.本文简要说明一下限制规则. strlen ...
- 清北刷题班day3 morning
P99zhx: 竞赛时间:???? 年?? 月?? 日??:??-??:??题目名称 a b c名称 a b c输入 a.in b.in c.in输出 a.out b.out c.out每个测试点时限 ...
- codevs1574广义斐波那契数列
1574 广义斐波那契数列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 广义的斐波那契数列是指形如an=p* ...
- [Swift通天遁地]八、媒体与动画-(8)使用开源类库快速实现位移动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- memcache缓存系统
一.缓存系统 静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服 ...
- Android 关于android.os.Build介绍
关于Build类的介绍 这个类为一个获取设备一些初始化信息的类,该类的主要信息都是通过一些static的字段获得: public static final String BOARD The name ...