Leetcode brushing notes (C++) - bit operation

Leetcode brushing notes (C++) - bit operation

Sort out the ideas in the process of brushing the questions, and summarize and share them here.
github address: https://github.com/lvjian0706/Leetcode-solutions
The github project is just newly created, and the organized code and ideas will be uploaded one after another. The code is based on C++ and python. At the same time, the basic sorting algorithm will also be sorted and uploaded.

136. Numbers that occur only once

Given a non-empty array of integers, each element appears twice except for a certain element that appears only once. Find the element that appears only once.

Explanation:
Your algorithm should have linear time complexity. Can you do it without using extra space?

Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4

class Solution {
    
    
public:
    /*
    由于时间和空间限制,所以使用二进制的异或运算解答即可;
    */
    int singleNumber(vector<int>& nums) {
    
    
        int ans = nums[0];
        for(int i=1; i<nums.size(); i++){
    
    
            ans ^= nums[i];
        }
        return ans;
    }
};

191. The number of bit 1

Write a function that takes an unsigned integer as input and returns the number of '1' digits in its binary representation (also known as the Hamming weight).

Example 1:
Input: 00000000000000000000000000001011
Output: 3
Explanation: In the input binary string 00000000000000000000000000001011, a total of three bits are '1'.
Example 2:
Input: 00000000000000000000000010000000
Output: 1
Explanation: In the input binary string 00000000000000000000000010000000, a total of one bit is '1'.
Example 3:
Input: 111111111111111111111111111101
Output: 31
Explanation: In the input binary string 111111111111111111111111111101, a total of 31 bits are '1'.

方法1class Solution {
    
    
public:
    /*
    返回二进制表达式中数字位数为 ‘1’ 的个数:
    求二进制数(对2取余后整除2),当该位为1时,ans++;
    */
    int hammingWeight(uint32_t n) {
    
    
        int ans = 0;
        while(n!=0){
    
    
            ans += (n % 2);
            n /= 2;
        }
        return ans;
    }
};

方法2class Solution {
    
    
public:
    /*
    返回二进制表达式中数字位数为 ‘1’ 的个数:
    n = n & (n-1) : 将最后一个1置0;
    将最后一个1循环置0,直到所有位上都是0;
    */
    int hammingWeight(uint32_t n) {
    
    
        int ans = 0;
        while(n){
    
    
            n = n & (n-1);
            ans++;
        }
        return ans;
    }
};

260. The number III that occurs only once

Given an integer array nums, exactly two elements appear only once, and all other elements appear twice. Find those two elements that appear only once.

Example:
Input: [1,2,1,3,2,5]
Output: [3,5]

class Solution {
    
    
public:
    /*
    找出只出现一次的那两个元素:因为是常数空间,考虑位运算:
    x & (-x) :保留位中最右边1,且将其余的1设为0。 
    1. xy ^= nums[i]:先找到只出现过一次的两个数的异或结果;
    2. dif = xy & (-xy):找出x与y不同的一位(最右边一位);
    3. x ^= nums[i]:找到x;
    4. xy^x:找到y;
    */
    vector<int> singleNumber(vector<int>& nums) {
    
    
        vector<int> ans;
        int xy=0;
        for(int i=0; i<nums.size(); i++){
    
    
            xy ^= nums[i];
        }
        int dif = xy & (-xy);
        int x = 0;
        for(int i=0; i<nums.size(); i++){
    
    
            if(nums[i]&dif){
    
    
                x ^= nums[i];
            }
        }
        ans.push_back(x);
        ans.push_back(xy^x);
        return ans;
    }
};

338. Counting Bits

Given a non-negative integer num. For each number i in the range 0 ≤ i ≤ num, count the number of 1s in its binary digit and return them as an array.

Example 1:
Input: 2
Output: [0,1,1]
Example 2:
Input: 5
Output: [0,1,1,2,1,2]

class Solution {
    
    
public:
    /*
    对于0≤i≤num范围中的每个数字i,计算其二进制数中的1的数目并将它们作为数组返回:动态规划问题
    1. 奇数:二进制表示中,奇数一定比前面那个偶数多一个 1,因为多的就是最低位的 1。
    2. 偶数:二进制表示中,偶数中 1 的个数一定和除以 2 之后的那个数一样多。因为最低位是 0,除以 2 就是右移一位,也就是把那个 0 抹掉而已,所以 1 的个数是不变的。
    */
    vector<int> countBits(int num) {
    
    
        vector<int> dp(num+1);
        dp[0] = 0;
        for(int i=1; i<num+1; i++){
    
    
            if(i%2==0){
    
    
                dp[i] = dp[i/2];
            }
            else{
    
    
                dp[i] = dp[i-1] + 1;
            }
        }
        return dp;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43273742/article/details/107740748
Recommended