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 ...
随机推荐
- 并查集 - BZOJ 1104 [POI2007]洪水
BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...
- 简单配置.htaccess就可以实现的10个功能
http://bbs.itcast.cn/thread-25990-1-1.html?xhw 1. 反盗链那些盗用了你的内容,还不愿意自己存储图片的网站是很常见的.你可以通过以下配置来放置别人盗用你的 ...
- python - 接口自动化 - 接口测试基础知识
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_接口测试基础知识一.py@ide: PyCharm Commun ...
- selenium - 常用页面操作
# 2.常用页面操作 # 访问某一个页面url = 'http://www.baidu.com'driver.get(url) # 获取页面的标题title = driver.titleprint(t ...
- linux随笔四
1.ps -ef : -e 显示系统上运行的所有进程,-f 显示一些有用的信息列 UID:负责启动进程的用户 PID:进程的ID PPID:父进程的PID(某个进程由另一个进程启动) C: ...
- Android library projects cannot be launched解决方法
着了一个例子项目,总是报标题说的错误. 解决方法如下: 红圈的地方,勾掉. 貌似如果你这个项目是作为一个被引用的project的话, 要勾上这个.单独作为一个app的话,不能勾选这个. --不懂,瞎写 ...
- Python爬虫selenium、PhanmJs
selenium:可以模拟鼠标进行一些操作 实例1:实现自动打开google浏览器,进行百度搜索,并关闭浏览器 from selenium import webdriver from time imp ...
- Windows上安装DB2——从IBM官网得到90天试用版
我在下面选的90天试用版: https://www.ibm.com/developerworks/cn/downloads/im/db2/ 进入下载页面,选择Windows https://www-0 ...
- [SDOI2013]直径 (树的直径,贪心)
题目链接 Solution 我们直接找到一条直径 \(s\),起点为 \(begin\),终点为 \(end\). 从前往后遍历点 \(u\) ,若子树中最大的距离与 \(dis(u,begin)\) ...
- FZOJ Problem 2110 Star
...