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 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.
翻译:
给定一个非负整数num,对于每一个0<=i<=num的整数i。计算i的二进制表示中1的个数,返回这些个数作为一个数组。
比如。输入num = 5 你应该返回 [0,1,1,2,1,2].
分析:
依照常规思路,非常容易得出“Java代码2”的方案。可是这个方法的时间复杂度是O(nlogn)。
通过对数组的前64个元素进行分析(num=63),我们发现数组呈现一定的规律,不断重复。例如以下图所看到的:
0
1
1 2
1 2 2 3
1 2 2 3 2 3 3 4
1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5
1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6
由此我们发现0112是一个基础元素。不断循环重复。能够推论:假设已知第一个元素是result[0],那么第二第三个元素为result[0]+1,第四个元素为result[0]+2,由此获得前4个元素result[0]~result[3]。以这4个元素为基础。我们能够得到
result[4]=result[0]+1,result[5]=result[1]+1…。
result[8]=result[0]+1,result[9]=result[1]+1… ,
result[12]=result[0]+2,result[13]=result[1]+2…;
以此类推能够获得所有的数组。
Java版代码1:
public class Solution {
public int[] countBits(int num) {
int[] result = new int[num + 1];
int range = 1;
result[0] = 0;
boolean stop = false;
while (!stop) {
stop = fillNum(result, range);
range *= 4;
}
return result;
}
public boolean fillNum(int[] nums, int range) {
for (int i = 0; i < range; i++) {
if (range + i < nums.length) {
nums[range + i] = nums[i] + 1;
} else {
return true;
}
if (2 * range + i < nums.length) {
nums[2 * range + i] = nums[i] + 1;
}
if (3 * range + i < nums.length) {
nums[3 * range + i] = nums[i] + 2;
}
}
return false;
}
}
Java版代码2:
public class Solution {
public int[] countBits(int num) {
int[] result=new int[num+1];
result[0]=0;
for(int i=1;i<=num;i++){
result[i]=getCount(i);
}
return result;
}
public int getCount(int num){
int count=0;
while(num!=0){
if((num&1)==1){
count++;
}
num/=2;
}
return count;
}
}
Leet Code OJ 338. Counting Bits [Difficulty: Medium]的更多相关文章
- 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 ...
- 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 ...
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 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 ...
随机推荐
- python - 接口自动化测试实战 - case1 - 优化版
题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...
- python 查看异常
接触python 一直觉着编译后报错经常没能捕捉显示,每次也只能从头看到尾 恰好在水木社区中看到关于异常捕捉帖子 方法一:捕获所有异常 try: a=b b=c except Exception,ex ...
- 零基础自学用Python 3开发网络爬虫
原文出处: Jecvay Notes (@Jecvay) 由于本学期好多神都选了Cisco网络课, 而我这等弱渣没选, 去蹭了一节发现讲的内容虽然我不懂但是还是无爱. 我想既然都本科就出来工作还是按照 ...
- ruby操作mysql
require "win32ole" require 'pathname' require 'mysql2' excel = WIN32OLE.new('excel.applica ...
- ie,360浏览器出现无法打开网页(包括本地html)的解决方法
有一天,编写网页照例打开chrome,ie,360等浏览器,发现ie,360均无法打开本地网页,输入百度,也无法打开,从没遇到过这种情况,通过百度,找了几种方法,没解决, 后来,看到有一种原因可能是浏 ...
- 解决在IE6、7中用height来设定SELECT标签高度无效的兼容性问题
在IE6.7中用height来设定SELECT标签高度是无效的,宽度的话各浏览器设置都是一致的,解决方法就是在select外嵌套两层标签,一层用来遮挡select的默认边框(在IE6.7中设置bord ...
- BZOJ 4819 [Sdoi2017]新生舞会 ——费用流 01分数规划
比值最大 分数规划 二分答案之后用费用流进行验证. 据说标称强行乘以1e7换成了整数的二分. 不过貌似实数二分也可以过. #include <map> #include <cmath ...
- P1382 楼房 (扫描线,线段树)
题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平线高度为0.在轮廓 ...
- Hadoop 3.1.0 在 Ubuntu 16.04 上安装时遇到的问题
1.Hadoop 安装 pdsh localhost: Connection refused Hadoop安装过程中使用 $ sbin/start-dfs.sh 启动节点时,发生错误提示: pdsh@ ...
- d3 根据数据绘制svg
, , , , ]; var circles = svg.selectAll("circle") .data(dataset) .enter() .append("cir ...