当前位置:网站首页>LeetCode异或运算
LeetCode异或运算
2022-04-23 19:42:00 【Garrick不想996】
-
异或运算的规则就是两个相同的数异或之后的结果为0,0和其他不为0的数异或之后的结果为后者,而且异或运算支持交换律和结合律,有了这些定义使得有些算法可以很巧妙的使用异或运算解决问题。
-
加法运算
异或可以不使用+号实现两个数相加,两个数异或的结果就是它们相加的结果
#include <iostream>
using namespace std;
int main() {
int single =20;
single ^=10;
cout<<single;
return 0;
}
输出结果为30
- 只出现一次的次数
也是最神奇的一个功能,也是因为这个题才写这个,LeetCode上一道题目:

class Solution {
public:
int singleNumber(vector<int>& nums) {
int temp = 0;
for(int num:nums){
temp = temp ^ num;
}
return temp;
}
};
- 异或运算有以下三个性质。
任何数和 0 做异或运算,结果仍然是原来的数,即 a⊕0=a。
任何数和其自身做异或运算,结果是 0,即 a⊕a=0。
异或运算满足交换律和结合律,即 a⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b
版权声明
本文为[Garrick不想996]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44537267/article/details/124230923
边栏推荐
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
- Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
- Build intelligent garbage classification applet based on Zero
- JVM的类加载过程
- The difference between underline and dot of golang import package
- filebeat、logstash配置安装
- 【webrtc】Add x264 encoder for CEF/Chromium
- 【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
- antd dropdown + modal + textarea导致的textarea光标不可被键盘控制问题
- Data analysis learning directory
猜你喜欢

Virtual machine performance monitoring and fault handling tools

Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies

2021-2022-2 ACM training team weekly Programming Competition (8) problem solution

antd dropdown + modal + textarea导致的textarea光标不可被键盘控制问题

Scrum Patterns之理解各种团队模式

@MapperScan与@Mapper

How to create bep-20 pass on BNB chain

Zero base to build profit taking away CPS platform official account

Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy

山大网安靶场实验平台项目-个人记录(五)
随机推荐
Electron入门教程3 ——进程通信
Unity general steps for creating a hyper realistic 3D scene
基于pytorch搭建GoogleNet神经网络用于花类识别
Some speculation about the decline of adults' language learning ability
Thoughts on the optimization of examination papers in the examination system
【webrtc】Add x264 encoder for CEF/Chromium
Go recursively loops through folders
Audio editing generation software
filebeat、logstash配置安装
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
MySQL syntax collation (4)
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
仓库管理数据库系统设计
如何在BNB鏈上創建BEP-20通證
[report] Microsoft: application of deep learning methods in speech enhancement
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)
Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
Use test of FFT and IFFT library functions of TI DSP
How about Bohai futures. Is it safe to open futures accounts?