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]相关推荐
- Leet Code OJ 268. Missing Number [Difficulty: Medium]
题目: Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is miss ...
- Leet Code OJ 136. Single Number [Difficulty: Medium]
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- Leet Code OJ 91. Decode Ways [Difficulty: Medium]
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Leet Code OJ 344. Reverse String [Difficulty: Easy]
题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...
- Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid. The brac ...
- Leet Code OJ 1. Two Sum [Difficulty: Easy]
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
最新文章
- BC#29A:GTY's math problem(math) B:GTY's birthday gift(矩阵快速幂)
- Tomcat - Tomcat 8.5.55 启动过程源码分析阶段二_load加载初始化
- eclipse版本详解以及下载安装步骤及启动eclispe报错整理
- Linux 调优方案, 修改最大连接数(ulimit命令)
- php 字符串 字典序序排序,C++ 怎么实现字典序排序法,自然排序
- matlab 价格统计,matlab中的金融数据统计
- Kubernetes[3]-Server
- Java阶段2-02JS:08ECMAScript BOM DOM:
- CF1110E Magic Stones(构造题)
- This project needs to migrate WTP metadata
- 服务器开机后显示f1 f2,电脑开机总是提示按f1 f2问题的解决办法
- Json Editor命令行版
- 【已测】开源PHP个人导航网站源码,有后台
- excel转word后表格超出页面_excel转word后表格显示不全
- Win10 笔记本 共享 wifi 热点
- 银行类app如何保证安全性
- 服务器winsxs文件夹怎么清理工具,win7系统如何使用WinSxS工具安全删除WinSxS文件夹垃圾?...
- android re卸载程序,手机自带软件卸载不了?教你2种方法,强制卸载预装应用程序!...
- 信息系统项目管理师必背核心考点(十)信息系统规划
- 微信小程序云函数使用教程【超详细】