当前位置:网站首页>2022-02-12 (338. Bit count)
2022-02-12 (338. Bit count)
2022-07-03 04:18:00 【TickTick123】
class Solution {
public int[] countBits(int n) {
int[] result=new int[n+1];
for(int i=0;i<=n;++i){
result[i]=oneCount(i);
}
return result;
}
public int oneCount(int x){
int count=0;
while(x!=0){
x=x&(x-1);
++count;
}
return count;
}
}
Dynamic programming :
class Solution {
public int[] countBits(int n) {
int[] result=new int[n+1];
for(int i=1;i<=n;++i){
result[i]=result[i&(i-1)]+1;
}
return result;
}
}
边栏推荐
- 跨境电商多商户系统怎么选
- 基于Pytorch和RDKit的QSAR模型建立脚本
- Introduction to eth
- Sklearn data preprocessing
- Square root of X
- Feature_selection
- MPLS setup experiment
- Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- How to connect WiFi with raspberry pie
猜你喜欢

用户体验五要素

『期末复习』16/32位微处理器(8086)基本寄存器
![[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius](/img/01/c118725f74e39742df021b5dbcc33b.jpg)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius

540. Single element in ordered array

【刷题篇】接雨水(一维)

leetcode:297. Serialization and deserialization of binary tree

【刷题篇】 找出第 K 小的数对距离

JS实现图片懒加载

Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences

Two points -leetcode-540 A single element in an ordered array
随机推荐
Introduction to eth
Xrandr modify resolution and refresh rate
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
Export of zip file
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
x Problem B
Idea shortcut keys
Sklearn data preprocessing
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
nodejs基础:浅聊url和querystring模块
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
IPv6 foundation construction experiment
Mongodb slow query optimization analysis strategy
深潜Kotlin协程(十九):Flow 概述
Drf--- quick start 01
[set theory] set concept and relationship (set family | set family examples | multiple sets)
[brush questions] most elements (super water king problem)