leetcode338—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 1:
Input: 2 Output: [0,1,1]
Example 2:
Input: 5
Output: [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.
想法:统计0-num之间的二进制表示中1的个数。用result[i]表示i的二进制表示中1的个数。利用动态规划,找出状态转移表达式result[i]=result[i&i-1]+1
class Solution {
public:
vector<int> countBits(int num) {
vector<);
result[] = ;
;i <= num ; i++){
result[i] = result[i & i-] + ;
}
return result;
}
};
leetcode338—Counting Bits的更多相关文章
- 【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 ...
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)
Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...
- 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 ...
- [Swift]LeetCode338. 比特位计数 | Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- 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)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- [LeetCode] Counting Bits 计数位
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
随机推荐
- HDU1069(KB12-C)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- python-适配器模式
源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 为了解决接口不兼容的问题引进一种接口的兼容机制,就是适配器模式,其通过提供一种适 ...
- 锋利的Jquery之插件Cookie记住密码
先下载Jquery cookie js ,下载路径: http://plugins.jquery.com/cookie/ 记住,jquery的包要放在cookie的包前面,否则会产生异常 <!D ...
- 如何使用活字格快速搭建Bug管理系统?
Bug管理系统是指一种用于添加Bug.修复Bug.测试Bug.删除Bug的一套完整的Bug管理系统. 完整的Bug管理过程包含: 1.测试人员利用Bug管理系统提交发现的bug. 2.测试人员把bug ...
- MySQL缓存机制详解(一)
本文章拿来学习用||参考资料:http://www.2cto.com/database/201308/236361.html 对MySql查询缓存及SQL Server过程缓存的理解及总结 一.M ...
- 犀牛Rhino教程合集37部
犀牛Rhino教程合集37部 教程说明:英文视频教程,部分有中文字幕,大部分有工程文件 教程格式:Flv.MP4格式,大部分高清,确保能看清软件上的文字 发货方式:百度网盘下载链接(教程较多,可转存到 ...
- apk安装提示:Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=XXX]
近日,楼主在同一台手机上,同时安装同一个游戏的不同渠道包,add install后,提示:Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=andro ...
- LeetCode题解之 3Sum
1.题目描述 2.问题分析 使用hashtable 的方法做,解法不是最优的,思路简单直观. 3.代码 vector<vector<int>> threeSum(vector& ...
- 使用python快速搭建本地网站
如果你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比如:Apache,ISS,Nodejs等.那么, Python 可能帮助你.使用Python可以完成一个简 ...
- C# socket 发送图片和文件
先说服务端:界面:如图: 界面设计源码 namespace SocketJPGToTxt { partial class Form1 { /// <summary> /// 必需的设计器变 ...