算法系类 -- 位运算

/**
 * User: Qkj
 * Description: 位运算
 * Date: 2023-07-02
 * Time: 23:33
 */
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
// 基础位运算

// https://leetcode.cn/problems/is-unique-lcci/
// 解法一: 使用hash
// 优化:  使用一个 26位的数组
// class Solution
// {
// public:
//   bool isUnique(string astr)
//   {
//     if (astr.empty())
//       return true;
//     unordered_set<char> set;
//     for (auto e : astr)
//       set.insert(e);
//     return set.size() == astr.size();
//   }
// };
// 继续优化: 使用 位图的思想
//
// class Solution
// {
// public:
//   bool isUnique(string astr)
//   {
//     // 利用鸽巢原理
//     if (astr.size() > 26)
//       return false;
//     int bitMap = 0;
//     for (auto e : astr)
//     {
//       int i = e - 'a';
//       if ((bitMap >> i) & 1 == 1)
//         return false;
//       else
//         bitMap |= 1 << i;
//     }
//     return true;
//   }
// };
// https://leetcode.cn/problems/missing-number/
// class Solution
// {
// public:
//   int missingNumber(vector<int> &nums)
//   {
//     int n = nums.size();
//     int result = 0;
//     for (int i = 0; i <= n; i++)
//       result ^= i;
//     for (int i = 0; i < n; i++)
//       result ^= nums[i];
//     return result;
//   }
// };
// https://leetcode.cn/problems/sum-of-two-integers/submissions/
// class Solution
// {
// public:
//   int getSum(int a, int b)
//   {
//     while (b != 0)
//     {
//       int ret = a ^ b;
//       unsigned int carry = (unsigned int)(a & b) << 1;
//       a = ret;
//       b = carry;
//     }
//     return a;
//   }
// };
// https://leetcode.cn/problems/single-number-ii/
// class Solution
// {
// public:
//   int singleNumber(vector<int> &nums)
//   {
//     int ret = 0;
//     for (int i = 0; i < 32; i++)
//     {
//       int sum = 0;
//       // 修改每一位
//       for (int j = 0; j < nums.size(); j++)
//       {
//         if ((nums[j] >> i) & 1 == 1)
//           sum++;
//       }
//       sum %= 3;
//       if (1 == sum)
//         ret |= 1 << i;
//     }
//     return ret;
//   }
// };
// https://leetcode.cn/problems/missing-two-lcci/submissions/
class Solution
{
public:
  vector<int> missingTwo(vector<int> &nums)
  {
    int tmp = 0;
    for (auto x : nums)
      tmp ^= x;
    for (int i = 1; i <= nums.size() + 2; i++)
      tmp ^= i;
    // 找出  a b 不同的哪一位
    int diff = 0;
    while (1)
    {
      if ((tmp >> diff) & 1 == 1)
        break;
    else
      diff++;
    }

    // 开始分类
    vector<int> result(2, 0);
    for (auto x : nums)
    {
      if ((x >> diff)&1 == 1)
        result[0] ^= x;
      else
        result[1] ^= x;
    }
    for (int i = 1; i <= nums.size() + 2; i++)
    {
      if ((i >> diff)&1 == 1)
        result[0] ^= i;
      else
        result[1] ^= i;
    }
    return result;
  }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄鸟轩墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值