algorithm-base/animation-simulation/求次数问题/只出现一次的数3.md

114 lines
4.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [260. III](https://leetcode-cn.com/problems/single-number-iii/)
> nums
:
> : [1,2,1,3,2,5]
> : [3,5]
1 1 2 1
### HashSet
####
HashSet
####
```java
class Solution {
public int[] singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for (int x : nums) {
//存在的则移除
if (set.contains(x)) {
set.remove(x);
continue;
}
//不存在存入
set.add(x);
}
//存到数组里,然后返回
int[] arr = new int[2];
int i = 0;
for (int y : set) {
arr[i++] = y;
}
return arr;
}
}
```
###
####
> **a,b,a,b,c,d,e,f,e,f **
>
> Aa, a , b, b, c c
>
> Be, e, f, f, d d
c , d () c , d A B
c , d 1 , 1
001 100 = 101 1 1 1 0 A 1 B
c , d 0 101 001 100
a & 1 a 0 1 101 001 x & 001 x x & 100 .
0 101 001
x & (-x) 1
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/分组位.25gbi25kv7c0.png)
####
```java
class Solution {
public int[] singleNumber(int[] nums) {
int temp = 0;
//求出异或值
for (int x : nums) {
temp ^= x;
}
//保留最右边的一个 1
int group = temp & (-temp);
System.out.println(group);
int[] arr = new int[2];
for (int y : nums) {
//分组位为0的组组内异或
if ((group & y) == 0) {
arr[0] ^= y;
//分组位为 1 的组,组内异或
} else {
arr[1] ^= y;
}
}
return arr;
}
}
```