LeetCode338——比特位计数

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/counting-bits/

题目描述:

知识点:位运算、动态规划

思路一:动态规划

状态定义:f(x) -------- 数组中索引为x处的值

状态转移

(1)如果x是偶数,f(x) = f(x / 2)。因为对于偶数来说,其乘以2只是在所有位左移1位并在右边新添一个0,因此其1的个数不会发生变化。

(2)如果x是奇数,f(x) = f(x - 1)。奇数的1的个数一定和比它小的最大偶数的1的个数相同。

时间复杂度和空间复杂度均是O(n)。

JAVA代码:

public class Solution {

    public int[] countBits(int num) {
        if (num == 0) {
            return new int[] {0};
        } else if (num == 1) {
            return new int[] {0, 1};
        }
        int[] result = new int[num + 1];
        result[0] = 0;
        result[1] = 1;
        for (int i = 2; i < num + 1; i++) {
            if (i % 2 == 0) {
                result[i] = result[i / 2];
            } else {
                result[i] = result[i - 1] + 1;
            }
        }
        return result;
    }

}

LeetCode解题报告:

思路二:位运算

i中1的个数一定比i & (i - 1)中1的个数多1个。

(1)如果i是奇数,显然i最右边的那位是1,而i & (i - 1)会消去最右边的那个1,而其余的1个数不变,1的总个数会减少1。

(2)如果i是偶数,显然i的最右边那位是0,而i & (i - 1)会使得最右边那位依然是0,而使得中间的某个1变为0。举个例子:

假设i = 00000000 00000000 00000000 10000000,那么

   i -1 = 00000000 00000000 00000000 01111111,显然i & (i - 1) = 00000000 00000000 00000000 00000000,1的总个数减少了1个。

时间复杂度和空间复杂度均是O(n)。

JAVA代码:

public class Solution {

    public int[] countBits(int num) {
        int[] result = new int[num + 1];
        result[0] = 0;
        for (int i = 1; i < num + 1; i++) {
            result[i] = result[i & (i - 1)] + 1;
        }
        return result;
    }

}

LeetCode解题报告:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值