随笔分类 - 第二期Week2——位运算专题讲解
摘要:1 struct Node 2 { 3 Node* next[2] = {nullptr}; 4 }; 5 6 class Solution 7 { 8 public: 9 void insert(int num, Node* root) 10 { 11 for (int i = 30; i >=
阅读全文
摘要:1 //0-31位中 每一位0的个数与1的个数乘积之和 2 class Solution 3 { 4 public: 5 int totalHammingDistance(vector<int>& nums) 6 { 7 int res = 0; 8 for(int i = 0;i < 32;i +
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int getSum(int a, int b) 5 { 6 if(!b) return a; 7 int sum = a ^ b,carry = (unsigned int)(a & b) << 1;//LeetCode编译器不支持
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int findComplement(int num) 5 { 6 int res = 0,t = 0; 7 while(num) 8 { 9 res += !(num & 1) << t; 10 num >>= 1,t ++; 11
阅读全文
摘要:1 class Solution 2 { 3 public: 4 unordered_set<int> hash = {2,3,5,7,11,13,17,19}; 5 int countPrimeSetBits(int L, int R) 6 { 7 int res = 0; 8 for(int i
阅读全文
摘要:1 class Solution 2 { 3 public: 4 vector<int> singleNumber(vector<int>& nums) 5 { 6 int diff = 0; 7 vector<int> res(2,0); 8 for (int num : nums) diff ^
阅读全文
摘要:1 class Solution 2 { 3 public: 4 bool isPowerOfTwo(int n) 5 { 6 return n > 0 && (n & n - 1) == 0; 7 } 8 }; 1 class Solution 2 { 3 public: 4 bool isPow
阅读全文
摘要:1 //n & n-1 总是去掉二进制最低位的1 从最高位&起来会快很多 2 class Solution 3 { 4 public: 5 int rangeBitwiseAnd(int m, int n) 6 { 7 //m <= n && m <= n - 1 ——> m <= n - 1 8
阅读全文
摘要:1 //用bits数组统计nums中所有数转化成二进制后,每个位上1的个数 2 //将bits数组中能够整除3的值置0,不能整除3的值置1 3 //将bits转换成真的二进制数 4 class Solution 5 { 6 public: 7 int singleNumber(vector<int>
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int singleNumber(vector<int>& nums) 5 { 6 int res = 0; 7 for(auto a : nums) res ^= a; 8 return res; 9 } 10 };
阅读全文

浙公网安备 33010602011771号