当前位置:网站首页>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)));
}
}
边栏推荐
- School day (summer vacation daily question 2)
- y76.第四章 Prometheus大厂监控体系及实战 -- prometheus进阶(七)
- R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
- HTB-Optimum
- Era5 dataset description
- How to start if you want to be a product manager?
- Difference between NPX and NPM
- Detailed explanation of stepn chain game system development mode (Sports money making mode)
- Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network
- sqlilabs less-28~less-8a
猜你喜欢

Promise implementation

求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!

sqlilabs less-28~less-8a

ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off

Talk about how redis handles requests

Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络

编程大杂烩(二)

Big talk · book sharing | Haas Internet of things device cloud integrated development framework

Leetcode 204. count prime numbers (wonderful)

编程大杂烩(一)
随机推荐
新时代生产力工具——FlowUs 息流全方位评测
Realsense D435i 深度图优化_高精度模式
Microservice configuration center Nacos
ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
(2022 Niuke multi school) D-Link with game glitch (SPFA)
基于ISO13209(OTX)实现EOL下线序列
(牛客多校二)G-Link with Monotonic Subsequence(构造题)
Mechanism and principle of multihead attention and masked attention
LCP plug-in creates peer-to-peer 802.1ad interface
HTB-Devel
编程大杂烩(二)
QT qtextedit setting qscrollbar style sheet does not take effect solution
2021年ICPC陕西省赛热身赛 B.CODE(位运算)
context must be a dict rather解决
The difference between $write and $display in SystemVerilog
Ffmpeg notes (I) fundamentals of audio and video
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
Concepts of phase velocity and phase in transmission line theory
leetcode/整数除法
剑指 Offer 54. 二叉搜索树的第k大节点