当前位置:网站首页>leetcode-位运算
leetcode-位运算
2022-07-29 10:44:00 【Pr Young】
判断一个数是否是2的n次方,比如16是,5不是
暴力法:
class Solution
{
public boolean isPowerOfTwo(int n)
{
for(int i=0;Math.pow(2,i)<=n;i++)
{
if(Math.pow(2,i)==n) return true;
}
return false;
}
}如果一个数m是2的n次方,那么它的二进制表示就是100000,最高位为1,其他位都是0
而m-1的二进制表示就是011111,最高位为0,其他位为1
所以m&m-1=0

class Solution
{
public boolean isPowerOfTwo(int n)
{
//提交时发现有两个测试用例0和-2147483648
//这两个预期都是false,但是我们答案得到的是true
if(n<=0) return false;
return (n&(n-1))==0;
}
}
核心:首先这个数得是2的n次方,其次这个数除以3余数必须为1,那么这个数就是4的幂
class Solution
{
public boolean isPowerOfFour(int n)
{
return ((n&(n-1))==0)&&(n%3==1);
}
}将输进来的数依次和下面32个数进行与操作,结果不为0说明输进来的数这一位是1,结果为0说明输进来的数这一位是0,用一个数count来统计位为1的位数

左移32次法:
public class Solution
{
// you need to treat n as an unsigned value
public int hammingWeight(int n)
{
int count=0;
int m=1;//32个数中的第一个数,0000.....0001
for(int i=1;i<=32;i++)
{
if((n&m)!=0) count++;
m=m<<1;//m左移1位
}
return count;
}
}当然你也可以不断的将输入的n进行右移一位,和0000.....0001来进行与操作,右移32次法:
public class Solution
{
// you need to treat n as an unsigned value
public int hammingWeight(int n)
{
int count=0;
int m=1;
for(int i=1;i<=32;i++)
{
if((n&m)!=0) count++;
n=n>>1;//n右移1位
}
return count;
}
}解法3:利用性质:n&(n-1),运算结果是把n的二进制表示中的最低位的1变成0:

利用这个性质,不断的将当前n和n-1进行&操作,直到n变成0
public class Solution
{
// you need to treat n as an unsigned value
public int hammingWeight(int n)
{
int count=0;
while(n!=0)
{
n=n&(n-1);
count++;
}
return count;
}
}面试题 16.01. 交换数字 - 力扣(LeetCode)
不使用中间变量交换两个数字


class Solution
{
public int[] swapNumbers(int[] numbers)
{
numbers[0] = numbers[0] ^ numbers[1];
numbers[1] = numbers[0] ^ numbers[1];
numbers[0] = numbers[0] ^ numbers[1];
return numbers;
}
}
数组中只出现一次的数(其他数都出现两次)
利用性质:(1)任何数和自己做异或(异为1,同为0),结果都为0
(2)0和任何数做异或,结果为本身
class Solution
{
public int singleNonDuplicate(int[] nums)
{
int result=0;
for(int num:nums)
{
result=result^num;
}
return result;
}
}也就是说先将两个数进行异或运算,然后就是上卖那道题目了:求位1的个数
class Solution
{
public int hammingDistance(int x, int y)
{
//先求出x^y,即x和y做异或运算就可以求出这两个数之间有多少位不一样了
//然后看结果中有几位1就知道汉明距离是多少了
//那怎么看一个数的二进制表示里面有几个1呢?x=x&(x-1)看几次x才能变为0
int temp=x^y;
int result=0;
while(temp!=0)
{
result++;
temp=temp&(temp-1);
}
return result;
}
}边栏推荐
- The heavy | open atomic school source activity was officially launched
- Implementation of college logistics repair application system based on SSM
- The server
- 专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
- mosquitto_ Sub -f parameter use
- DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022
- Meeting OA project (V) -- meeting notice and feedback details
- 1. (map tools) detailed tutorial of acrgis desktop10.5 software installation
- Luogu p1816 loyalty solution
- How can agile development reduce cognitive bias in collaboration| Agile way
猜你喜欢

阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!

How to realize the function of adding watermark

VMWare:使用命令更新或升级 VMWare ESXi 主机

12th generation core processor +2.8k OLED ASUS good screen, lingyao 142022 shadow cyan glaze business thin book

QT基本工程的解析

LeetCode二叉树系列——144.二叉树的前序遍历

Conference OA project - my approval

How can agile development reduce cognitive bias in collaboration| Agile way

DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022

Summer 2022 software innovation laboratory training JDBC
随机推荐
Svn revision keyword
若依集成minio实现分布式文件存储
【Unity,C#】Character键盘输入转向与旋转
ADB shell WM command and usage:
Summer 2022 software innovation laboratory training JDBC
8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
R language uses data set veteran for survival analysis
Start from scratch blazor server (3) -- add cookie authorization
What happens when MySQL tables change from compressed tables to ordinary tables
2022cuda summer training camp Day2 practice
ggdag 绘制DAG和因果图
周鸿祎:360是世界上最大的安全大数据公司
Structure the eighth operation of the combat battalion module
R package pedquant realizes stock download and financial quantitative analysis
The heavy | open atomic school source activity was officially launched
[QNX hypervisor 2.2 user manual]7.2.1 hypervisor tracking events
Consumer electronics, frozen to death in summer
AI模型风险评估 第2部分:核心内容
Data visualization design guide (information chart)
Research on the realization of linear gradient circular progress bar
