Leet Code OJ 338. Counting Bits [Difficulty:easy]

Source: Internet
Author: User

Topic:
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 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 I do it in linear time O (n)/possibly in a single pass?
Space complexity should be O (n).
Can do it like a boss? Do it without the using any builtin function like __builtin_popcount in C + + or any other language.
Hint:
You should make the use of the produced already.

Translation:
Given a non-negative integer num, for each 0<=i<=num integer i, computes the number of the binary representation of I in 1, returning these numbers as an array.
For example, enter num = 5 and you should return [0,1,1,2,1,2].

Analysis:
It is easy to get the "Java Code 2" scheme in the usual way, but the time complexity of this scheme is O (NLOGN).
By analyzing the first 64 elements of an array (num=63), we find that the arrays are rendered in a certain regularity, repeating as shown:

Thus we find that 0112 is a basic element, which is repeated repeatedly and can be inferred: if the first element is known as result[0], then the second third element is result[0]+1, and the fourth element is result[0]+2, thus obtaining the first 4 elements result[0]~ RESULT[3]; Based on these 4 elements, we can get
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 ...;
And so on, you get all the arrays.

Java version Code 1:

 Public  class solution {     Public int[]countbits(intNUM) {int[] result =New int[Num +1];intRange =1; result[0] =0;BooleanStop =false; while(!stop)            {stop = Fillnum (result, range); Range *=4; }returnResult } Public Boolean Fillnum(int[] Nums,intRange) { for(inti =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 Version Code 2:

 Public classSolution { Public int[]countbits(intNUM) {int[] result=New int[num+1]; result[0]=0; for(intI=1; i<=num;i++) {result[i]=getcount (i); }returnResult } Public int GetCount(intNUM) {intCount=0; while(num!=0){if((num&1)==1) {count++; } num/=2; }returnCount }}

Leet Code OJ 338. Counting Bits [Difficulty:easy]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.