【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 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.
Credits:
Special thanks to @ syedeefor adding this problem and creating all test cases.
解法一:
按照定义做,注意,x&(x-1)可以消去最右边的1
class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret;
for(int i = ; i <= num; i ++)
ret.push_back(countbit(i));
return ret;
}
int countbit(int i)
{
int count = ;
while(i)
{
i &= (i-);
count ++;
}
return count;
}
};
解法二:
对于[2^k, 2^(k+1)-1]区间,可以划分成前后两部分
前半部分与[2^(k-1), 2^k-1]内的值相同,后半部分与[2^(k-1), 2^k-1]内值+1相同。
class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret; ret.push_back();
if(num == )
// start case 1
return ret;
ret.push_back(); if(num == )
// start case 2
return ret; // general case
else
{
int k = ;
int result = ;
while(result- < num)
{
result *= ;
k ++;
}
// to here, num∈ [2^(k-1),2^k-1]
int gap = pow(2.0, k) - - num;
for(int i = ; i < k-; i ++)
{// infer from [2^i, 2^(i+1)-1] to [2^(i+1), 2^(i+2)-1]
// copy part
for(int j = pow(2.0, i); j <= pow(2.0, i+)-; j ++)
ret.push_back(ret[j]);
// plus 1 part
for(int j = pow(2.0, i); j <= pow(2.0, i+)-; j ++)
ret.push_back(ret[j]+);
}
for(int i = ; i < pow(2.0, k-) - gap; i ++)
{
int j = pow(2.0, k-) + i;
if(i < pow(2.0, k-))
// copy part
ret.push_back(ret[j]);
else
// plus 1 part
ret.push_back(ret[j]+);
}
}
return ret;
}
};
【LeetCode】338. Counting Bits (2 solutions)的更多相关文章
- 【leetcode】338 .Counting Bits
原题 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate t ...
- 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【LeetCode】190. Reverse Bits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 【Leetcode】338. Bit位计数
每次刷leetcode都有一种发现新大陆的感觉. 题目链接:https://leetcode-cn.com/problems/counting-bits/description/ 给定一个非负整数 n ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- java经典50编程题
菲波拉契数列:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? package com.day2; public ...
- Java递归输出指定路径下所有文件及文件夹
package a.ab; import java.io.File; import java.io.IOException; public class AE { public static void ...
- JAVA中的重载和重写
重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载(Overloading)是一个类中多态性的一种表现 ...
- C++ map使用(基于RBTree)
一.insert ◦1)用insert函数插入pair数据 ◦map<int, string> mapStudent; ◦mapStudent.insert(pair<int, st ...
- iOS 关于nil和Nil及null与<null>的区别
问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadin ...
- OpenGL(三)——函数大全
概述 根据自己写的小程序对各个函数进行解释 OpenGL函数 1. 颜色 1.1 glShadeModel 绘制指定两点间其他点颜色的过渡模式 没试 1.2 glColor 设置当前颜色:可以派生 ...
- delphi调试需要管理员权限程序报错“Unable to create process:请求的操作需要提升”
delphi调试启动需要UAC权限的程序的时候会报错“Unable to create process:请求的操作需要提升”.这是因为delphi没有以管理员身份启动,这样delphi createp ...
- 真实世界:使用WCF扩展记录服务调用时间
WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服 ...
- [安卓] 5、SeekBar拖动条
越来越发现这些控件用法大同小异了,这里注意几个函数:seekBar.setSecondaryProgress(0);设置初始进度为0,总共为0~99,对其监听用setOnSeekBarChangeLi ...
- [WinAPI] API 10 [创建、打开、读写文件,获取文件大小]
在Windows系统中,创建和打开文件都是使用API函数CreateFile,CreateFile通过指定不同的参数来表示是新建一个文件,打开已经存在的文件,还是重新建立文件等.读写文件最为直接的方式 ...