编程语言
首页 > 编程语言> > leetcode算法之位运算

leetcode算法之位运算

作者:互联网

今天来盘一盘 **位运算 ** 这类题目

使用python刷题分类整理的笔记,请参考: https://github.com/lxztju/leetcode-algorithm/tree/v1

位运算

461 汉明距离 (Easy)

class Solution {
public:
    int hammingDistance(int x, int y) {
    // 统计不同的位数
        auto sx = bitTransfer(x);
        auto sy = bitTransfer(y);
        int cnt = 0;
        for ( int i = 0; i< 32; i++){
            if (sy[i] != sx[i])
                cnt++;
        }
        return cnt;

    }
    string bitTransfer(int x){
        // 将转换为01的二进制字符串
        string s(32, '0');
        if (x == 0) return s;
        int i = 31;
        while ( x > 0){
            int res = x % 2;
            s[i] = (res + '0');
            x /= 2;
            i--;
        }
        return s;
    }
};
class Solution {
public:
    int hammingDistance(int x, int y) {
    // 利用异或操作,不同的为1, 然后统计1的个数
        int res = x ^ y;
        int cnt = 0;
        while ( res > 0){
            cnt += (res & 1);
            res >>= 1;
        }
        return cnt;

    }

};

136 只出现一次的数字 (Easy)

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for (auto num:nums){
            res ^= num;
        }
        return res;
    }
};

268 丢失的数字 (Easy)

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int res = 0;
        for ( int i=0; i< nums.size(); i++){
            res ^= (nums[i] ^ i);
        }
        return res ^ nums.size();
    }
};

260 只出现一次的数字 III (Medium)

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        // 整个数组进行异或操作,得到两个单独出现的字符的异或值
        int numXor = 0;
        for (auto num: nums)
            numXor ^= num;

        // 按照这个异或者,将原始数组分为两部分,每个单独出现的数字分别位于其中的一部分。
        int split = 1;
        while ((numXor & split) == 0){
            split <<= 1;
        }
        vector<int> res(2, 0);
        for (auto num : nums){
            if (num & split)
                res[0] ^= num;
            else
                res[1] ^= num;
        }
        return res;
    }
};

190 颠倒二进制位 (Easy)

class Solution {
  public:
  uint32_t reverseBits(uint32_t n) {
    uint32_t res = 0, power = 31;
    while (n != 0) {
      res += (n & 1) << power;
      n >>= 1;
      power -= 1;
    }
    return res;
  }
};

231 2的幂 (Easy)

class Solution {
public:
    bool isPowerOfTwo(int n) {
        int res = 0;
        int cnt = 0;
        if ( n < 0) return false;
        while ( n != 0){
            if ((n & 1) == 1)
                cnt++;
            n >>= 1;
        }
        return cnt == 1;
    }
};
class Solution {
public:
    bool isPowerOfTwo(int n) {
        return ( n > 0 && (( n & (n - 1) )== 0) );
    }
};

342 4的幂 (Easy)

class Solution {
public:
    bool isPowerOfFour(int num) {
    	// 2的幂
        if (num > 0 && (num & ( num - 1 )) == 0)
        	// 判断是否是4的幂
            return (num & 0x55555555) == num;
        return false;
    }
};

693 交替位二进制数 (Easy)

class Solution {
public:
    bool hasAlternatingBits(int n) {
        int tmp1 = n & 1;
        n >>= 1;
        while ( n != 0){
            int tmp2 = n & 1;
            n >>= 1;
            if (tmp1 == tmp2)
                return false;
            tmp1 = tmp2;
        }
        return true;
    }
};

476 数字的补数 (Easy)

class Solution {
public:
    int findComplement(int num) {
        long tmp = 1;
        while( tmp <= num){
            tmp <<= 1;
        }
        return tmp - 1 - num;
    }
};

371 两整数之和 (Easy)

class Solution {
public:
    int getSum(int a, int b) {
        return a + b;
    }
};
class Solution {
public:
    int getSum(int a, int b) {

        while (b){
            auto carry = (static_cast<unsigned int> (a & b) )<< 1;
            a = a ^ b;
            b = carry;
        }
        return a;
    }
};

318 最大单词长度乘积 (Medium)

class Solution {
public:
    int maxProduct(vector<string>& words) {
        // 使用一个整形数字来表每个单词所含有字母的种类。,这个整形数字的后26位,如果某一位为1,说明出现这个字母
        int n = words.size();
        vector<int> code(n, 0);
        for (int i = 0; i < n; i++){
            for (int j=0; j < words[i].size(); j++){
                code[i] |= 1 << (words[i][j] - 'a');
            }
        }
        int res = 0;
        for ( int i = 0; i < n-1; i++){
            for ( int j = i+1; j < n; j++){
                if ((code[i] & code[j]) == 0){
                    int tmp = (words[i].size()) * (words[j].size());
                    res = max(res, tmp);
                }
            }
        }
        return res;
    }
};

338 比特位计数 (Medium)

class Solution {
public:
    vector<int> countBits(int num) {
        // 直接统计
        vector<int> res;
        for (int i = 0; i<=num; i++){
            int tmp = 0;
            int tmp1 = i;
            while (tmp1 != 0){
                tmp += (tmp1 & 1);
                tmp1 >>= 1;
            }
            res.push_back(tmp);
        }
        return res;
    }
};

更多分类刷题资料

标签:return,int,res,之位,public,算法,num,Easy,leetcode
来源: https://blog.csdn.net/lxztju/article/details/113815432