LeetCode Q338 Counting Bits(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.

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.

翻译:给定一个非负整数num,求出0至num中每个数的二进制中1的个数。

分析:

解法一:

  暴力算法,用一个函数算出每个数的二进制中1的个数,但没有满足题目要求。

解法二:

  O(n)的算法,说明算出每一个数的答案必定和前面的有关系。

二进制                  1的个数

0000                        0

0001                        1

0010                        1

0011                        2

0100                        1

0101                        2

0110                        2

0111                        3

……                        ……

  第一种规律,n的二进制中1的个数等于n-4的二进制中1的个数加一。

  如果把最后一位于前面的分开呢?

二进制                  1的个数

000  0                      0

000  1                      1

001  0                      1

001  1                      2

010  0                      1

010  1                      2

011  0                      2

011  1                      3

……                        ……

  动态规划的思想,因为右移一位的数必定比它小,所以之前必定算过,只需要看最后一位就行了。满足题目一切条件。

 1 public class Solution
 2 {
 3     public int[] CountBits(int num)
 4     {
 5         int[] ret=new int[num+1];
 6         ret[0]=0;
 7         for (int i=1;i<=num;i++) ret[i]=ret[i>>1]+i%2;
 8         return ret;
 9     }
10 }
时间: 2024-08-11 07:49:02

LeetCode Q338 Counting Bits(Medium)的相关文章

leetCode 338. Counting Bits | Dynamic Programming | Medium

338. 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]. 题目大意

338. Counting Bits [medium] (Python)

题目链接 https://leetcode.com/problems/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

【Leetcode】Counting Bits

题目链接:https://leetcode.com/problems/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 y

Leetcode 338 – Counting Bits

解法三种: Sln 1. 笨办法,移位.时间复杂度太高,基本就是O(n * sizesof(int)).Sln 2. 利用一个小技巧,偶数除二,在其另外一个乘数左移 1bit而得:奇数在前所得上加1Sln 3. Hamming Weight; 利弊: Sln 1不考虑,复杂度过高.相对来说Sln 2要较Sln 3简单,但是仅仅刷个算法题并没什么特别大的用处,还要考虑一下实际的应用场景.如果我们使用Sln 2,则如果要随机取一个数的二进制1 的个数,需要一个很大的查找表.Sln 3则是随时可用.

leetcode 338. Counting Bits,剑指offer二进制中1的个数

leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种方法,利用奇偶性找规律 class Solution { public: vector<int> countBits(int num) { vector<int> result{0}; for(int i = 1;i <= num;i++){ if(i % 2 == 0) res

LeetCode 338. 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 e

338. Counting Bits (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 e

【LeetCode】Counting Bits(338)

1. Description 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]. 2. Answ

leetcode(1)--338.Counting Bits

LeetCode 338. 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,