leetcode bitmap系列问题整理

1、

题目:

编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

示例 :

输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011

示例 2:

输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011 

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count=0;
        while(n>0){
            n = n&(n-1);
            count++;
        }
        return count;
    }
};

 

2、Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?

 求是否是4的次方。
4的次方数有个特点,首先必须大于0; 4的次方数的二进制中,1在奇数位置。而且4的次方数的二进制中只有1个1。例如
1 == 001      4的0次方
4 == 100
16 == 10000
64 == 16<<2 == 1000000
 
class Solution {
public:
    bool isPowerOfFour(int num) {
        //首先  num&(num-1) 必须是0并且1在奇数位置 01010101  01010101   01010101  01010101
        return num>0 && !(num&(num-1)) && (num&(0x55555555));
    }
};

 

3、Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1
class Solution {
public:
    //其实就是二进制按位加
    // 0010= 2
    // 1010= 12
    // 1(1)10=14
    //看上面的例子:结果括号中的1,第二位是进位1。
    //要知道 a&b的结果中1的位置就是求和会产生进位的位置,例如 (2+3)
    //10
    //11     10&11=10,只有第二位会产生进位。
    //我们只要迭代利用进位的结果,再与a^b的结果进行求和就能算出下一个进位和a^b.
    //直到所有的二进制和不再有进位,那么结果直接按位或即可。
    //重点:a&b的结果左移一位才是纯进位后的结果。
    
    
    //(-1&1)   -1: 10000001
    int getSum(int a, int b) {
        while(b != 0){
    //进位的结果LeetCode 自己的编译器比较严,不能对负数进行左移,就是说最高位符号位必须要为0,才能左移
            unsigned carry = (a&b);
            //无进位位置上的和。
            a = a^b;
            //更新b.
            b = carry<<1;
        }
        return a;
    }
};

 

4、Missing Number

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

 

Example 1:

Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.

Example 2:

Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.

Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.

Example 4:

Input: nums = [0]
Output: 1
Explanation: n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.

 

Constraints:

  • n == nums.length
  • 1 <= n <= 104
  • 0 <= nums[i] <= n
  • All the numbers of nums are unique.
class Solution {
public:
    //异或。a^a = 0
    int missingNumber(vector<int>& nums) {
        int res=nums.size();
        for(int i=0;i<nums.size();i++){
            res ^= i;
            res ^= nums[i];
        }
        return res;
    }
};

 

5、Largest Power of 2 That Is Less Than the Given Number

 

posted on 2020-11-07 10:10  wsw_seu  阅读(242)  评论(0编辑  收藏  举报

导航