Leetcode: 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.
Hint:
- You should make use of what you have produced already.
- Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
- Or does the odd/even status of the number help you in calculating the number of 1s?
1 public class Solution { 2 public int[] countBits(int num) { 3 int[] res = new int[num+1]; 4 for (int i=1; i<=num; i++) { 5 res[i] = res[i>>1] + (i&1); 6 } 7 return res; 8 } 9 }
转载于:https://www.cnblogs.com/EdwardLiu/p/6092304.html
Leetcode: Counting Bits相关推荐
- LeetCode Counting Bits(动态规划)
问题:给出数字n,求0-n这些数的1的位数. 思路:方法一使用x&(x-1)统计数字1的位数. 方法二[0,1) [2,3)表示形式为[10,11) [4,8)表示形式为[100,101, 1 ...
- leetcode 上的Counting Bits 总结
最近准备刷 leetcode 做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1); //把二进制最左边那 ...
- [swift] 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
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- Counting Bits
https://leetcode.com/problems/counting-bits/ Given a non negative integer number num. For every numb ...
- [刷题]Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- 338. Counting Bits
为什么80%的码农都做不了架构师?>>> 想法:用一个掩码获取除最高位之外的剩下的几位 其中掩码分别是 0,2-1,4-1,8-1,16-1,... 当mask = 7,x = ...
- LeetCode - Reverse Bits
二进制转换和字符串逆序.要考虑int的范围,测试数据是有溢出的.Math.pow是有精度损失的,最好写成整数的. public class ReverseBits {public static int ...
- 338. Counting Bits(动态规划)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
最新文章
- 大数据实验报告总结体会_大数据挖掘流程及方法总结
- 40种为网页设计师准备的高品质和免费的多媒体PSD文件
- SpringBoot-(6)-日志SLF4j
- Cookie,Session的区别
- 巅峰极客2021 what_pickle——一道综合性的python web
- 钱币掉落动画android,mpvue实现小程序签到金币掉落动画(api实现)
- Datawha组队——Pandas(下)综合练习(打卡)
- LeetCode 889. 已知前序后序 求二叉树(不唯一)
- servlet html js提交表单,使用jquery.form.js实现form表单无刷新提交简单示例
- js复制 兼容浏览器
- @ab测试工具使用详解
- 线性四叉树的实现C++
- 无需格式化 移动硬盘/U盘上装WinPE、Win7PE图解
- 【python】报错:OSError: [Errno 30] Read-only file system
- android的EditText字数检测和限制
- android webView加载页面时显示出全部网页内容
- 二阶魔方还原 - 4步2公式
- 7-2 动物爱吃什么?
- 如何群发邮件?群发邮件让发收件人互相不知道?邮箱群发邮件技巧
- java中的几个术语(覆写override,隐藏hiding,重载overload,遮蔽shadowing,遮盖obscuring)
热门文章
- postgis安装_从零开始,构建电子地图网站:0_2_数据处理postgis
- html5 测评游戏,暗黑之王评测:HTML5游戏铸就最华丽ARPG冒险
- java获取byte 长度_java获取字节的长度.
- mysql数据库应用的权限层级_MySQL数据库的用户权限管理
- android 录音原始文件_音频采集:Android基于AudioRecord的实现
- matlab中CH指标聚类评价指标,MATLAB聚类有效性评价指标(外部)
- 双向a*搜索算法_双向搜索算法
- scala 函数中嵌套函数_Scala合成函数
- console java_Java Console writer()方法与示例
- 面向对象(封装对象private关键字)