当前位置:网站首页>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 three ways to copy files
- Esp8266 - beginner level Chapter 1
- First experience of using fluent canvas
- Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
- Go modules daily use
- Audio signal processing and coding - 2.5.3 the discrete cosine transform
- 一个简单的(基于redisson的)分布式同步工具类封装
- 【webrtc】Add x264 encoder for CEF/Chromium
猜你喜欢
指针数组与数组指针的区分
[报告] Microsoft :Application of deep learning methods in speech enhancement
NiO related Basics
Shanda Wangan shooting range experimental platform project - personal record (IV)
5 minutes to achieve wechat cloud applet payment function (including source code)
Class loading process of JVM
山大网安靶场实验平台项目-个人记录(五)
Esp8266 - beginner level Chapter 1
Intuitive understanding of the essence of two-dimensional rotation
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
随机推荐
Summary of several relationships of UML class diagram
Hot reload debugging
A simple (redisson based) distributed synchronization tool class encapsulation
[webrtc] add x264 encoder for CEF / Chromium
MySQL practical skills
IIS data conversion problem: 16bit to 24bit
Go modules daily use
如何在BNB链上创建BEP-20通证
Unity general steps for creating a hyper realistic 3D scene
MySQL syntax collation (4)
Reflection on the performance of some OpenGL operations in the past
Golang timer
精简CUDA教程——CUDA Driver API
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
Devops integration - environment variables and building tools of Jenkins service
How about Bohai futures. Is it safe to open futures accounts?
Zero cost, zero foundation, build profitable film and television applet
机器学习目录