当前位置:网站首页>leetcode/ 前 n 个数字二进制中 1 的个数
leetcode/ 前 n 个数字二进制中 1 的个数
2022-07-25 05:43:00 【xcrj】
package com.xcrj;
import java.util.Arrays;
/** * 剑指 Offer II 003. 前 n 个数字二进制中 1 的个数 * 给定一个非负整数 n ,请计算 0 到 n 之间的每个数字的二进制表示中 1 的个数,并输出一个数组。 * <p> * 考点: * 发现规律(动态规划) * r&1奇偶特性:r为奇数结果为1,r为偶数结果为0 * r>>1奇偶特性:r为奇数或偶数结果相等,结果都是除以2取整 */
public class Solution3 {
// 内存和耗时都大
// % /运算符
public int[] countBits(int n) {
int[] result = new int[n + 1];
// t负责运算,r负责索引
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;
}
// 内存和耗时都大
// >> 运算符
public int[] countBits2(int n) {
int[] result = new int[n + 1];
// t负责运算,r负责索引
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;
}
// 内存和耗时都大
// & 按位与 << 左移
public int[] countBits3(int n) {
int[] result = new int[n + 1];
// t负责运算,r负责索引
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;
}
/** * 动态规划+位运算 * dp[2n+1]-1=dp[2n]:奇数一定比前一个数(偶数)多1,二进制表示末尾的1 * dp[2n]=dp[n]:偶数一定比此偶数小一半的数,1的个数一样多。2n是偶数,2n/2=n是偶数,偶数的二进制表示最后1位都是0,除2相当于右移1位 */
public int[] countBits4(int n) {
int[] result = new int[n + 1];
result[0] = 0;
for (int r = 1; r < n + 1; r++) {
// 偶数
if (0 == r % 2) {
result[r] = result[r >> 1];
} else {
/** * r为奇数时 * result[r] = result[r - 1] + 1; * r-1就是偶数 * result[r - 1]=result[(r - 1)>>1] * (r - 1)>>1等于r>>1 * result[(r - 1)>>1]=result[r>>1] * 所以 * result[r] = result[r >>1]+1 */
result[r] = result[r >> 1] + 1;
}
}
return result;
}
/** * 动态规划+位运算 * dp[2n+1]-1=dp[2n]:奇数一定比前一个数(偶数)多1,二进制表示末尾的1 * dp[2n]=dp[n]:偶数一定比此偶数小一半的数,1的个数一样多。2n是偶数,2n/2=n是偶数,偶数的二进制表示最后1位都是0,除2相当于右移1位 */
public int[] countBits5(int n) {
int[] result = new int[n + 1];
result[0] = 0;
for (int r = 1; r < n + 1; r++) {
/** * 统一 result[r >> 1]和result[r - 1] * * r为奇数时 * result[r] = result[r - 1] + 1; * r-1就是偶数 * result[r - 1]=result[(r - 1)>>1] * (r - 1)>>1等于r>>1 * result[(r - 1)>>1]=result[r>>1] * 所以 * result[r] = result[r >>1]+1 */
// if (0 == r % 2) {
// result[r] = result[r >> 1];
// } else {
// result[r] = result[r >> 1] + 1;
// }
/** * 去掉奇偶判断 * 当r是偶数,不+1 * 当r是奇数,要+1 * * r是偶数时,r&1=0 * r是奇数时,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)));
}
}
边栏推荐
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- Leetcode 204. 计数质数(太妙了)
- Realsense d435i depth map optimization_ High precision mode
- HTB-Optimum
- Microservice configuration center Nacos
- SystemVerilog中interface(接口)介绍
- HTB-Beep
- Samsung folding screen has sent samples to apple and Google, and the annual production capacity will be expanded from 2.4 million to 10million!
- 微服务 - 网关Gateway组件
- Calculate BDP value and wnd value
猜你喜欢

HTB-Arctic

Vim查找替换及正则表达式的使用

New discovery of ROS callback function

Three schemes for finclip to realize wechat authorized login

Linear algebra (3)

Microservice - hystrix fuse

Unity accesses chartandgraph chart plug-in

编程大杂烩(二)

Why is it that all the games are pseudorandom and can't make true random?
![Atof(), atoi(), atol() functions [detailed]](/img/5a/a421eab897061c61467c272f122202.jpg)
Atof(), atoi(), atol() functions [detailed]
随机推荐
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
Switch NPM source to private source library
Difference between NPX and NPM
Productivity tool in the new era -- flowus information flow comprehensive evaluation
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
SystemVerilog中$write与$display区别
Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool
Sword finger offer special shock edition day 9
R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (set the hJust parameter to show that labels of all data points are a
Zhou Chen, vice president of zhanrui market, responded to everything about 5g chip chunteng 510!
New discovery of ROS callback function
Leetcode 202. happy number (not happy at all)
批量下载视频小技巧
Y76. Chapter IV Prometheus large factory monitoring system and practice -- Prometheus advanced (VII)
Obj file format and.Mtl file format
Programming hodgepodge (I)
Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
After Oracle user a deletes a table under user B's name, can user B recover the deleted table through the recycle bin?
Leetcode 237. delete nodes in the linked list
The selection reference of Junzheng T41, T40 and T31 versions are all here