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

269 lines
8.0 KiB
Java
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden 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 2
### HashSet
####
HashSet
####
Java Code:
```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);
//不存在则存入
} else {
set.add(x);
}
}
//存到数组里,然后返回
int[] arr = new int[2];
int i = 0;
for (int y : set) {
arr[i++] = y;
}
return arr;
}
}
```
C++ Code:
```cpp
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
unordered_set<int> set;
for (int x : nums) {
//存在的则移除
if (set.find(x) == set.end()) {
set.insert(x);
//不存在则存入
} else {
set.erase(x);
}
}
//存到数组里,然后返回
vector<int> arr;
for (int y : set) {
arr.push_back(y);
}
return arr;
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
let set = new Set();
for (let x of nums) {
//存在的则移除
if (set.has(x)) {
set.delete(x);
//不存在则存入
} else {
set.add(x);
}
}
//存到数组里,然后返回
let arr = [];
for (let y of set) {
arr.push(y);
}
return arr;
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
set_ = set()
for x in nums:
#
if x in set_:
set_.remove(x)
#
else:
set_.add(x)
#
arr = []
for y in set_:
arr.append(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
cd cd A B
cd 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 Code:
```java
class Solution {
public int[] singleNumber(int[] nums) {
//小心溢出
long temp = 0;
//求出异或值
for (int x : nums) {
temp ^= x;
}
System.out.println(temp);
System.out.println(-temp);
//保留最右边的一个 1
long group = temp & (-temp);
int[] arr = new int[2];
for (int y : nums) {
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
//分组位为 1 的组,组内异或
} else {
arr[1] ^= y;
}
}
return arr;
}
}
```
C++ Code:
```cpp
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
//小心 -temp 溢出
long temp = 0;
//求出异或值
for (int x : nums) {
temp ^= x;
}
//保留最右边的一个 1
int group = temp & (-temp);
vector<int> arr(2, 0);
for (int y : nums) {
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
//分组位为 1 的组,组内异或
} else {
arr[1] ^= y;
}
}
return arr;
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
let temp = 0;
//求出异或值
for (let x of nums) {
temp ^= x;
}
//保留最右边的一个 1
let group = temp & (-temp);
let arr = [0, 0];
for (let y of nums) {
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
//分组位为 1 的组,组内异或
} else {
arr[1] ^= y;
}
}
return arr;
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
temp = 0
#
for x in nums:
temp ^= x
# 1
group = temp & (-temp)
arr = [0, 0]
for y in nums:
# 0
if (group & y) == 0:
arr[0] ^= y
# 1
else:
arr[1] ^= y
return arr
```