文章目录
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ x, y < 231.
Example:
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
------↑— ↑
The above arrows point to positions where the corresponding bits are different.
方法一:逐位比较
class Solution {
public:
int hammingDistance(int x, int y) {
int count=0;
while(x||y)
{
if(x%2!=y%2)
count++;
x=x>>1;
y=y>>1;
}
return count;
}
};
方法二:两数异或,不同的位会保留下来
class Solution {
public:
int hammingDistance(int x, int y) {
x=x^y;
int count=0;
while(x)
{
if(x%2)
count++;
x=x>>1;
}
return count;
}
};
2 数组中唯一一个不重复的数
136. Single Number(Easy)
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for(auto c : nums)
res = res^c;
return res;
}
};
3 寻找数组中缺失的数
268. Missing Number(Easy)
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
方法一:不缺失的和减去数组和
class Solution {
public:
int missingNumber(vector<int>& nums) {
int cnt=0;
for(int i=nums.size()-1;i>=0;i--)
cnt+=i+1-nums[i];
return cnt;
}
};
方法二:位运算
将数组中的所有数和原本不缺失的所有数异或,除了缺失的数,其他都出现两次,所以结果为缺失的数。
例:Value 0 1 3 4
missing
=0∧(1∧0)∧(2∧1)∧(3∧3)∧(4∧4)
=(0∧0)∧(1∧1)∧(3∧3)∧(4∧4)∧2
=0∧0∧0∧0∧2
=2
class Solution {
public:
int missingNumber(vector<int>& nums) {
int cnt=0;
for(int i=0;i<nums.size();i++)
cnt=cnt^(i+1)^nums[i];
return cnt;
}
};
4 不重复的两个数字
260. Single Number III(Medium)
数组中有两个不重复的数字,找出这两个不重复的数字。
Example:
Input: [1,2,1,3,2,5]
Output: [3,5]
解题思路:
将所有元素异或,得到这两个不重复元素的异或值,从低位向高位找出第一个不相等的位(异或值该位等于1)
按照该位等于0和等于1,将元素分为两个组,分别异或就可以找出两个不相等的元素了。
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int diff = 0;
for (int c : nums)
diff ^= c;
int flag = 1;
while (1){
if (diff&flag)
break;
flag = flag<<1;
}
vector<int>res(2,0);
for(auto c : nums){
if (c&flag)
res[0] ^= c;
else
res[1] ^=c;
}
return res;
}
};
5 翻转一个数的比特位
4 判断一个数是不是 2 的 n 次方
231. Power of Two(Easy)
如果是 2 的 n 次方,则二级制数中只有一个 2。
class Solution {
public:
bool isPowerOfTwo(int n) {
return n>0 && (n&(n-1))==0;
}
};
可以直接统计二进制数中 1 的个数。
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<0)
return false;
int count=0;
while(n)
{
if(n&1)
count++;
n=n>>1;
}
return count==1?true:false;
}
};
5 判断一个数是不是 4 的 n 次方
342. Power of Four(Easy)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example 1:
Input: 16
Output: true
Example 2:
Input: 5
Output: false
Follow up: Could you solve it without loops/recursion?
问题分析:
要使得数是 4 的 n 次方,该数的二进制数中只有一个 1,且在奇数位置上。
class Solution {
public:
bool isPowerOfFour(int num) {
return num>0 && (num&(num-1))==0 && (num&0b01010101010101010101010101010101)!=0;
}
};
用循环统计二进制数奇数位,偶数位 1 的个数,要使得奇数位有 1 个 1,偶数位都为 0 才是 4 的 n 次方。
class Solution {
public:
bool isPowerOfFour(int num) {
int oddcount=0;
int evencount=0;
int tmp=num>>1;
while(num>0){
if(num&1)oddcount++;
if(tmp&1)evencount++;
num=num>>2;
tmp=tmp>>2;
}
return oddcount==1&&evencount==0;
}
};
直接除以 4,每次除以 4 之前判断对 4 求余是否有余数。
class Solution {
public:
bool isPowerOfFour(int num) {
if(num==0) return false;
while(num!=1){
if(num%4)
return false;
num=num/4;
}
return true;
}
};
6 查看整数二进制位 0 和 1 是否交替出现
693. Binary Number with Alternating Bits(Easy)
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.
Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.
Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
方法一:
如果 n 的二进制位为 0,1 交错,将 n 右移一位与自身相“异或”,则结果全为 1。结果加 1 之后和自身“与”为 0。
class Solution {
public:
bool hasAlternatingBits(int n) {
long a=n^(n>>1);
return (a&(a+1))==0;
}
};
方法二:
直接查看二对二进制数的相邻位进行比较,都不相同返回 true。
class Solution {
public:
bool hasAlternatingBits(int n) {
int i1=n&1;
n=n>>1;
int i2=n&1;
while(n>0){
if(i1==i2)return false;
i1=i2;
n=n>>1;
i2=n&1;
}
return true;
}
};