2021-5-3
一、英语
1、intrepidity:大胆、大胆的行为
2、A lady’s imagination is very rapid;it jumps from admiration to love,from love to matrimony in a moment.
3、wish sombody joy:向某人道贺
二、算法题目
1、693:位运算+二进制特点
class Solution {
public:
//方法一:位运算
/*
bool hasAlternatingBits(int n) {
int pre = n&1;//最后一位
n= n>>1;
while(n>0)
{
//注意位运算最好打上括号
if(pre==(n&1))
return false;
pre = n&1;
n = n>>1;
}
return true;
}*/
//方法二:利用二进制运算
//n为01交替,那么n^(n>>1)一定全部为1
//那么n&(n+1)==0
bool hasAlternatingBits(int n)
{
long long num = n^(n>>1);
return (num&(num+1))==0;
}
};
2、476:位运算
class Solution {
public:
//取补,关键是判断哪些位置为0,然后将相应位置置为1
//判断哪些位置为0可以用右移加上&1
//置为1,使用异或
//位数对齐,利用数字反转那道题的方式
/*
int findComplement(int num) {
int res = 0;
int tmp = 1;
while(num>0)
{
if((num&1)==0)
res = res|tmp;
tmp = tmp<<1;
num = num>>1;
}
return res;
}*/
//与1异或就是取反
int findComplement(int num)
{
int tmp = 1;
while(tmp<num)
{
tmp = tmp<<1;
tmp += 1;
}
return (tmp^num);
}
};
加油加油!