当前位置:网站首页>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
边栏推荐
- Go modules daily use
- 机器学习目录
- 5 minutes to achieve wechat cloud applet payment function (including source code)
- Kubernetes getting started to proficient - install openelb on kubernetes
- Summary of several relationships of UML class diagram
- 【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
- 点云数据集常用处理
- FFT物理意义: 1024点FFT就是1024个实数,实际进入fft的输入是1024个复数(虚部为0),输出也是1024个复数,有效的数据是前512个复数
- MFC获取本机IP(网络通讯时用得多)
- An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
猜你喜欢
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Prefer composition to inheritance
php参考手册String(7.2千字)
The platinum library cannot search the debug process records of some projection devices
Class loading mechanism
Grafana 分享带可变参数的链接
山大网安靶场实验平台项目-个人记录(五)
Class loading process of JVM
Understanding various team patterns in scrum patterns
随机推荐
[transfer] summary of new features of js-es6 (one picture)
C学习完结
The difference between underline and dot of golang import package
HTTP cache - HTTP authoritative guide Chapter VII
Kubernetes getting started to proficient - install openelb on kubernetes
Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
PHP reference manual string (7.2000 words)
Understanding various team patterns in scrum patterns
Translation of audio signal processing and coding: Preface
Speex维纳滤波与超几何分布的改写
深度学习环境搭建步骤—gpu
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Video understanding
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
IIS data conversion problem: 16bit to 24bit
TI DSP的 FFT与IFFT库函数的使用测试
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
Unity创建超写实三维场景的一般步骤