位运算(Bit Manipulation)一直是程序员面试中的一个必须准备的主题, 不过现在面试中位运算出现的次数并不多,主要原因还是位运算太考察技巧了,很多时候很难在短时间内想出来,所以作为面试的题目显得有点太花时间了。
位运算的主要思想是五种运算:与(&),或(|),异或(^),左移(<<),右移(>>)。
位运算的常用技巧如下:
LeetCode中关于位运算的题目有以下四种类型题:
(一)位操作之汉明距离(Hamming weight)相关题目:
(二)位操作之单个数(Single Number)相关题目:
(三)位操作之反转相关题目:
(四)位操作之数学相关题目:
191. Number of 1 Bits
00000000000000000000000000001011
, so the function should return 3.class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
res++;
n = n & (n-1);
}
return res;
}
};
461. Hamming Distance
x
and y
, calculate the Hamming distance.class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0, temp = x ^ y;
while(temp){
res++;
temp = temp & (temp-1);
}
return res;
}
};
477. Total Hamming Distance
class Solution {
public:
int totalHammingDistance(vector& nums) {
int res = 0;
for(int i = 0; i < 31; i++){
int cnt = 0;
for(auto n : nums){
if(n & 1<
136. Single Number
class Solution {
public:
int singleNumber(vector& nums) {
int res = 0;
for(auto i : nums){
res ^= i;
}
return res;
}
};
137. Single Number II
class Solution {
public:
int singleNumber(vector& nums) {
int res = 0;
for(int i = 0; i < 32; i++){
int temp = 0;
for(auto n : nums){
temp += (n>>i) & 1;
}
temp %= 3;
res |= (temp<
260. Single Number III
nums
, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.class Solution {
public:
vector singleNumber(vector& nums) {
vector res(2, 0);
int temp = 0;
for(auto n : nums){
temp ^= n;
}
int flag = temp^(temp&(temp-1));
for(auto n : nums){
if(flag & n) res[0] ^= n;
else res[1] ^= n;
}
return res;
}
};
190. Reverse Bits
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for(int i = 0; i < 32; i++){
uint32_t temp = (n>>i) & 1;
res |= (temp<<(31-i));
}
return res;
}
};
476. Number Complement
class Solution {
public:
int findComplement(int num) {
int res = 0, i = 0;
while(num){
int temp = (num & 1) ^ 1;
res |= (temp << i++);
num = num >> 1;
}
return res;
}
};
201. Bitwise AND of Numbers Range
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int res = 0, i = 0;
while(m != n){
m >>= 1;
n >>= 1;
i++;
}
res = m << i;
return res;
}
};
318. Maximum Product of Word Lengths
words
, find the maximum value of length(word[i]) * length(word[j])
where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.class Solution {
public:
int maxProduct(vector& words) {
int n = words.size(), res = 0;
vector mask(n, 0);
for(int i = 0; i < n; i++){
for(auto c : words[i]) mask[i] |= 1 << (c - 'a');
for(int j = 0; j < i; j++){
if((mask[i] & mask[j]) == 0){
res = max(res, int(words[i].size()*words[j].size()));
}
}
}
return res;
}
};
371. Sum of Two Integers
+
and -
. Example:Given a = 1 and b = 2, return 3.class Solution {
public:
int getSum(int a, int b) {
int sum = a;
while(b){
sum = a ^ b;
b = (a & b) << 1;
a = sum;
}
return sum;
}
};
29. Divide Two Integers
class Solution {
public:
int divide(int dividend, int divisor) {
long long dvd = abs((long long)dividend), dvs = abs((long long)divisor), res = 0;
if (dvd < dvs) return 0;
while(dvd >= dvs){
long long m = dvs, n = 1;
while(dvd > (m << 1)){
m <<= 1;
n <<= 1;
}
res += n;
dvd -= m;
}
if((dividend < 0)^(divisor < 0)) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};
总结:
这篇总结介绍了LeetCode中几类关于位运算的题目,虽然数量不多,不过思想上还是都挺有意义的,如果面试中遇到能提出位运算的解法还是能加分不少,所以位运算在有些题目中还是一把关键的武器。
如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来~