当前位置:网站首页>Leetcode/ number of 1 in the first n digit binary
Leetcode/ number of 1 in the first n digit binary
2022-07-25 05:45:00 【xcrj】
package com.xcrj;
import java.util.Arrays;
/** * The finger of the sword Offer II 003. front n A number in binary 1 The number of * Given a nonnegative integer n , Please calculate 0 To n In the binary representation of each number between 1 The number of , And output an array . * <p> * Examination site : * Found that regular ( Dynamic programming ) * r&1 Parity property :r Is an odd number, and the result is 1,r Is an even number, and the result is 0 * r>>1 Parity property :r For odd or even numbers, the result is equal , The result is divided by 2 integer */
public class Solution3 {
// Both memory and time consuming
// % / Operator
public int[] countBits(int n) {
int[] result = new int[n + 1];
// t In charge of calculation ,r Index
for (int r = 0; r < n + 1; r++) {
int t = r;
while (true) {
int remainder = t % 2;
if (1 == remainder) result[r]++;
int quotient = t / 2;
if (1 == quotient) {
result[r]++;
}
if (quotient < 2) {
break;
}
t = quotient;
}
}
return result;
}
// Both memory and time consuming
// >> Operator
public int[] countBits2(int n) {
int[] result = new int[n + 1];
// t In charge of calculation ,r Index
for (int r = 0; r < n + 1; r++) {
int t = r;
while (true) {
int remainder = t % 2;
if (1 == remainder) result[r]++;
int quotient = t >> 1;
if (1 == quotient) {
result[r]++;
}
if (quotient < 2) {
break;
}
t = quotient;
}
}
return result;
}
// Both memory and time consuming
// & Bitwise AND << Move left
public int[] countBits3(int n) {
int[] result = new int[n + 1];
// t In charge of calculation ,r Index
for (int r = 0; r < n + 1; r++) {
int t = r;
for (int i = 0; i < 32; i++) {
if (0 != (t & 1 << i)) {
result[r]++;
}
}
}
return result;
}
/** * Dynamic programming + An operation * dp[2n+1]-1=dp[2n]: Odd numbers must be better than the previous number ( even numbers ) many 1, Binary represents the end of 1 * dp[2n]=dp[n]: An even number must be half smaller than this even number ,1 There are as many as there are .2n It's even ,2n/2=n It's even , Even binary represents the last 1 All places are 0, except 2 It's equivalent to moving right 1 position */
public int[] countBits4(int n) {
int[] result = new int[n + 1];
result[0] = 0;
for (int r = 1; r < n + 1; r++) {
// even numbers
if (0 == r % 2) {
result[r] = result[r >> 1];
} else {
/** * r In an odd number of * result[r] = result[r - 1] + 1; * r-1 Even numbers * result[r - 1]=result[(r - 1)>>1] * (r - 1)>>1 be equal to r>>1 * result[(r - 1)>>1]=result[r>>1] * therefore * result[r] = result[r >>1]+1 */
result[r] = result[r >> 1] + 1;
}
}
return result;
}
/** * Dynamic programming + An operation * dp[2n+1]-1=dp[2n]: Odd numbers must be better than the previous number ( even numbers ) many 1, Binary represents the end of 1 * dp[2n]=dp[n]: An even number must be half smaller than this even number ,1 There are as many as there are .2n It's even ,2n/2=n It's even , Even binary represents the last 1 All places are 0, except 2 It's equivalent to moving right 1 position */
public int[] countBits5(int n) {
int[] result = new int[n + 1];
result[0] = 0;
for (int r = 1; r < n + 1; r++) {
/** * Unified result[r >> 1] and result[r - 1] * * r In an odd number of * result[r] = result[r - 1] + 1; * r-1 Even numbers * result[r - 1]=result[(r - 1)>>1] * (r - 1)>>1 be equal to r>>1 * result[(r - 1)>>1]=result[r>>1] * therefore * result[r] = result[r >>1]+1 */
// if (0 == r % 2) {
// result[r] = result[r >> 1];
// } else {
// result[r] = result[r >> 1] + 1;
// }
/** * Remove parity judgment * When r It's even , No +1 * When r Is odd , want +1 * * r When it's even ,r&1=0 * r It's an odd number ,r&1=1 */
result[r] = result[r >> 1] + (r & 1);
}
return result;
}
public static void main(String[] args) {
Solution3 solution3 = new Solution3();
System.out.println(Arrays.toString(solution3.countBits5(5)));
}
}
边栏推荐
- What are the ways to realize web digital visualization?
- Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool
- 求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!
- Unity接入ChartAndGraph图表插件
- A little experience about von Mises distribution
- Samsung folding screen has sent samples to apple and Google, and the annual production capacity will be expanded from 2.4 million to 10million!
- Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure
- LCP plug-in creates peer-to-peer physical interface
- 剑指 Offer 54. 二叉搜索树的第k大节点
- An SQL execution process
猜你喜欢

剑指 Offer 45. 把数组排成最小的数

Xiaomi 12s UTRA Leica watermark generation online tool

线性代数(三)

Siggraph 2022 -- rendering iridescent rock dove neck feathers

50 places are limited to open | with the news of oceanbase's annual press conference coming!

Working principle and precautions of bubble water level gauge
![(15)[驱动开发]过写拷贝](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15)[驱动开发]过写拷贝

Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network

The selection reference of Junzheng T41, T40 and T31 versions are all here

Realsense D435i 深度图优化_高精度模式
随机推荐
LCP plug-in creates peer VLAN interface
Leetcode 204. count prime numbers (wonderful)
sqlilabs less-29
School day (summer vacation daily question 2)
The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet
HTB-Devel
编程大杂烩(一)
Introduction summary of using unirx in unity
LCP plug-in creates peer-to-peer 802.1ad interface
HTB-Granpa
Leetcode 202. happy number (not happy at all)
Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络
y76.第四章 Prometheus大厂监控体系及实战 -- prometheus进阶(七)
Flexible layout summary
MATLAB作图实例:5:双轴图
PHP warehouse inventory management system source code WMS source code
PostgreSQL learning 04 PG_ hint_ Plan installation and use, SQL optimization knowledge
sqlilabs less-29
idea常用10个快捷键
传输线理论之相速、相位等的概念