You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
2.9 KiB
127 lines
2.9 KiB
6 years ago
|
---
|
||
|
异或应用
|
||
|
---
|
||
|
|
||
|
#### 目录
|
||
|
|
||
|
1. 思维导图
|
||
|
2. 概述
|
||
|
3. 常见问题解决
|
||
|
4. 参考
|
||
|
|
||
|
#### 思维导图
|
||
|
|
||
|
![](https://i.loli.net/2019/01/11/5c37f3306e40c.png)
|
||
|
|
||
|
#### 概述
|
||
|
|
||
|
异或运算就是相同的元素运算结果等于 0,不相同的元素异或等于 1。
|
||
|
|
||
|
基于此,异或有以下特性:
|
||
|
|
||
|
1. a^a = 0
|
||
|
|
||
|
相同的数异或为零。
|
||
|
|
||
|
2. a^0 = a
|
||
|
|
||
|
任何数与零异都为其本身。
|
||
|
|
||
|
3. a = a ^ b ^ b
|
||
|
|
||
|
#### 常见问题
|
||
|
|
||
|
##### 1. 不用中间变量交换 a 和 b
|
||
|
|
||
|
```java
|
||
|
public static void main(String[] args) {
|
||
|
int a = 2333;
|
||
|
int b = 1234;
|
||
|
a = a ^ b;
|
||
|
b = a ^ b;
|
||
|
a = a ^ b;
|
||
|
System.out.println(a + " " + b);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
##### 2. 一个整形数组,存 1 到 n 的所有整数,但缺少一个数,求这个数?
|
||
|
|
||
|
```java
|
||
|
public static void main(String[] args) {
|
||
|
int[] ints = new int[233];
|
||
|
for (int i = 0; i < 233; i++) {
|
||
|
ints[i] = i;
|
||
|
}
|
||
|
ints[23] = 0;
|
||
|
int result = 0;
|
||
|
for (int i = 0; i < ints.length; i++) {
|
||
|
result = i ^ result;
|
||
|
}
|
||
|
for (int i : ints) {
|
||
|
result = i ^ result;
|
||
|
}
|
||
|
System.out.println(result);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
##### 3. 一个整形数组除了一个数只出现一次,其他都出现两次,求这个数?
|
||
|
|
||
|
```java
|
||
|
public static void main(String[] args) {
|
||
|
int[] ints = new int[]{2, 3, 4, 5, 3, 4, 5, 7, 2, 9, 0, 7, 0};
|
||
|
int result = 0;
|
||
|
for (int i : ints) {
|
||
|
result = result ^ i;
|
||
|
}
|
||
|
System.out.println(result);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
##### 4. 一个整形数组除了两个数只出现一次,其他都出现两次,求这两个数?
|
||
|
|
||
|
```java
|
||
|
public static void main(String[] args) {
|
||
|
int[] ints = new int[]{2, 5, 2, 9, 7, 5, 6, 9};
|
||
|
int num = 0;
|
||
|
for (int i : ints) {
|
||
|
num = num ^ i;
|
||
|
}
|
||
|
ArrayList<Integer> list = new ArrayList<>();
|
||
|
for (int i : ints) {
|
||
|
if (list.contains(num ^ i)) {
|
||
|
list.remove(Integer.valueOf(num ^ i));
|
||
|
} else {
|
||
|
list.add(num ^ i);
|
||
|
}
|
||
|
}
|
||
|
System.out.println(list.get(0));
|
||
|
System.out.println(list.get(1));
|
||
|
}
|
||
|
```
|
||
|
|
||
|
##### 5. 一个整形数组除了三个数只出现一次,其他都出现两次,求这三个数?
|
||
|
|
||
|
```java
|
||
|
public static void main(String[] args) {
|
||
|
int[] ints = new int[]{2, 5, 2, 9, 7, 5, 6, 9, 8};
|
||
|
int num = 0;
|
||
|
for (int i : ints) {
|
||
|
num = num ^ i;
|
||
|
}
|
||
|
ArrayList<Integer> list = new ArrayList<>();
|
||
|
for (int i : ints) {
|
||
|
if (list.contains(num ^ i)) {
|
||
|
list.remove(Integer.valueOf(num ^ i));
|
||
|
} else {
|
||
|
list.add(num ^ i);
|
||
|
}
|
||
|
}
|
||
|
System.out.println(list.get(0) ^ num);
|
||
|
System.out.println(list.get(1) ^ num);
|
||
|
System.out.println(list.get(2) ^ num);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
#### 参考
|
||
|
|
||
|
[异或应用小总结](https://blog.csdn.net/hulamua/article/details/52411446)
|