位运算

文章目录

  • 位运算
    • LeetCode 231. Power of Two
    • LeetCode 762. Prime Number of Set Bits in Binary Representation
    • LeetCode 136. Single Number
    • LeetCode 476. Number Complement
    • LeetCode 137. Single Number II
    • LeetCode 260. Single Number III
    • LeetCode 371. Sum of Two Integers
    • LeetCode 201. Bitwise AND of Numbers Range
    • LeetCode 477. Total Hamming Distance
    • LeetCode 421. Maximum XOR of Two Numbers in an Array

LeetCode 231. Power of Two

class Solution {public:bool isPowerOfTwo(int n) {return n>0 && (1 << 30)%n == 0;//return (n>0 && (n & -n) == n);}
};
//lowbit,取出n二进制中第一个1的位置k
/*
lowbit(n) = n & ( ~ n + 1 )
又在补码中,~n+1= - n,所以
lowbit(n) = n & (- n)
那lowbit有什么应用呢?我们可以求得一个数字二进制中所有1的个数,从而得到0的个数……
https://www.cnblogs.com/CuteAbacus/p/9464358.html
*/

LeetCode 762. Prime Number of Set Bits in Binary Representation

class Solution {public:int countPrimeSetBits(int L, int R) {unordered_set<int> primes({2,3,5,7,11,13,17,19});int res = 0;for(int i = L; i <= R; i++){int s = 0;int k = i;while(k){s += k & 1;k >>= 1;}if(primes.count(s))res++;}return res;}
};
//判断1的个数

LeetCode 136. Single Number

class Solution {public:int singleNumber(vector<int>& nums) {if(nums.size() == 0)return 0;int res = nums[0];for(int i = 1; i < nums.size(); i++){res ^= nums[i];}return res;}
};
//异或的性质,但是如果留下两个呢?

LeetCode 476. Number Complement

class Solution {public:int findComplement(int num) {int res = 0;int t = 0;while(num){res += (!(num & 1)) << t;num >>= 1;t++;}return res;}
};
//取1

LeetCode 137. Single Number II

class Solution {public:int singleNumber(vector<int>& nums) {//方法一:// int ans = 0;// for(int i = 0; i < 32; i++)// {//     int count = 0;//     for(int j = 0; j < nums.size(); j++)//     {//         count += nums[j] >> i & 1;//     }//     ans += (count%3) << i;// }// return ans;//神仙做法://状态机,初始为0,0//1个1, 1 0; 2个1, 0 1; 3个1, 0 0int ones = 0, twos = 0;for(auto x : nums){ones = (ones ^ x) & ~twos;twos = (twos ^ x) & ~ones;}return ones;}
};

LeetCode 260. Single Number III

class Solution {public:vector<int> singleNumber(vector<int>& nums) {//得到两个数异或的结果int s = 0;for(auto x : nums) s ^= x;//找到第k位,是不是可以用lowbitint k = 0;while(!(s >> k & 1))k++;//分组异或int s2 = 0;for(auto x : nums){if(x >> k & 1)s2 ^= x;}int s3 = s ^ s2;return vector<int>({s2, s3});}
};
//这里用到了取最低位

LeetCode 371. Sum of Two Integers

class Solution {public:int getSum(int a, int b) {if(!b)return a;int sum = a ^ b;unsigned int carry = (unsigned int)(a & b) << 1;return getSum(sum, carry);}
};
//carry要是无符号整形,对负数有效

LeetCode 201. Bitwise AND of Numbers Range

class Solution {public:int rangeBitwiseAnd(int m, int n) {int res = 0;for(int i = 0; (1ll << i) <= m; i++){if(m >> i & 1){if((m & ~((1<<i)-1ll)) + (1 << i) > n )res += 1 << i;}}return res;}
};
//这题没看懂

LeetCode 477. Total Hamming Distance

class Solution {public:int totalHammingDistance(vector<int>& nums) {int res = 0;for(int i = 0; i <= 30; i++){int ones = 0;for(auto x : nums){if(x >> i & 1)ones++;}res += ones * (nums.size() - ones);}return res;}
};
//这题关键是巧妙的汉明距离,变成了某一个1个个数和0的个数的乘积

LeetCode 421. Maximum XOR of Two Numbers in an Array

class Solution {public:struct Node{int son[2];};vector<Node> nodes; int findMaximumXOR(vector<int>& nums) {nodes.push_back(Node({0,0}));for(auto x : nums){int p = 0;for(int i = 30; i >= 0; i--){int t = x >> i & 1;if(!nodes[p].son[t]){nodes.push_back(Node{0,0});nodes[p].son[t] = nodes.size() - 1;}p = nodes[p].son[t];}}int res = 0;for(auto x : nums){int p = 0, XOR = 0;for(int i = 30; i >= 0; i--){int t = x >> i & 1;if(nodes[p].son[!t]){p = nodes[p].son[!t];XOR += 1 << i;}else{p = nodes[p].son[t];}}res = max(res, XOR);}return res;    }
};
//关键建立一个trie树,然后从最高位开始找不同

leetcode-位运算相关推荐

  1. LeetCode位运算(找出落单的数,二进制中1的个数,2的幂等)

    文章目录 位运算理论+技巧介绍 1.与 & 2.异或 ^ 3.移位及综合运用(指定位置) 4.同或 开撸 1. lc136 只出现一次的数字 2. lc137 只出现一次的数字II 3. lc ...

  2. leetcode位运算的题

    leetcode 371 不用加减计算两个数的和 class Solution(object):def getSum(self, a, b):"""circuit ful ...

  3. 【LeetCode·位运算.67】二进制求和,详解分析+两种思路+知识点总结

    二进制求和

  4. JavaScript 位运算总结拾遗

    最近补充了一些位运算的知识,深感位运算的博大精深,此文作为这个系列的总结篇,在此回顾下所学的位运算知识和应用,同时也补充下前文中没有提到的一些位运算知识. 把一个数变为大于等于该数的最小的2的幂 一个 ...

  5. LeetCode:位运算实现加法

    LeetCode:位运算实现加法 写在前面 位运算符 实现加法的思路 两个加数,比如5(101)和6(110),如何不用加法就能得出两者之和呢? 我们知道二进制计算中,如果使用异或将会产生无进位的两者 ...

  6. [Leetcode][第78题][JAVA][子集][位运算][回溯]

    [问题描述][中等] [解答思路] 1. 位运算 复杂度 class Solution {List<Integer> t = new ArrayList<Integer>(); ...

  7. [Leetcode][第461题][JAVA][汉明距离][位运算][Brian Kernighan]

    [问题描述][简单] [解答思路] 1. 内置位计数功能 时间复杂度:O(1) 空间复杂度:O(1) class Solution {public int hammingDistance(int x, ...

  8. [Leetcode][第201题][JAVA][数字范围按位与][位运算][Brian Kernighan]

    [问题描述][中等] [解答思路] 1. 暴力 逐位与 ,只需要判断i= 0 或 i == 2147483647 的话,就跳出 for 循环即可. 时间复杂度:O(N) 空间复杂度:O(1) publ ...

  9. LeetCode 2032. 至少在两个数组中出现的值(哈希/位运算)

    文章目录 1. 题目 2. 解题 2.1 哈希查找 2.2 位运算 1. 题目 给你三个整数数组 nums1.nums2 和 nums3 ,请你构造并返回一个 不同 数组,且由 至少 在 两个 数组中 ...

  10. LeetCode 320. 列举单词的全部缩写(回溯/位运算)

    文章目录 1. 题目 2. 解题 2.1 回溯 2.2 位运算 1. 题目 请你写出一个能够举单词全部缩写的函数. 注意:输出的顺序并不重要. 示例: 输入: "word" 输出: ...

最新文章

  1. 驱动中获取PsActiveProcessHead变量地址的五种方法
  2. TCP/IP详解--第八章
  3. 织梦换了html模板样式没了,织梦(dedecms)如何更换网站模板?
  4. 关于ERP、MES、SFC问题
  5. 多对多(many-to-many)
  6. java如何记住登录状态_Spring security实现记住我下次自动登录功能过程详解
  7. linux对于图形方式的运行级,在大多数Linux发行版本中,图形方式的运行级定义为( )?...
  8. Atitit jquery  1.4--v1.11  v1.12  v2.0  3.0 的新特性
  9. 力软 Learun 是如何验证权限的
  10. 超级终端连接华为交换机_Win8系统使用超级终端连接华为交换机的方法
  11. oracle报错ora-12162,sqlplus登录提示:ORA-12162错误
  12. veeam备份linux,VeeamBackup Replication 创建备份任务
  13. 【CCF】小中大(C++)
  14. Illustrator CS5序列号
  15. 基于微型计算机系统的报警器设计,防盗报警器的设计毕业设计分析.doc
  16. AppCan推插件AppCan-EC:电商网站可DIY手机客户端
  17. ESP8266 WIFI模块学习之路(2)——模块与单片机连接进行远程操作
  18. 归一化php,归一化函数normalize详解
  19. 信创操作系统--麒麟Kylin桌面版 (项目七 网络连接:有线、无线网络)
  20. CONVERT 函数的用法

热门文章

  1. python常用常用函数
  2. 好工具推荐系列:看图软件irfanview/Honeyview/pineapple-pictures
  3. 如果NBA也像JAVA一样面试
  4. 184. 部门工资最高的员工
  5. axios是干什么的
  6. 微信小程序导入csv文件乱码问题
  7. 解决Notepad--在ubuntu16.04无法运行问题
  8. Linux配置SAP JCO驱动
  9. 用计算机写作 说课稿,高中英语说课稿:《Module 2 Unit 3 Computer》优秀说课稿范例...
  10. 创新驱动国产介入医疗设备崛起 唯迈医疗携极光平板DSA亮相新疆介入放射学年会