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

269 lines
8.0 KiB
Java
Raw Normal View History

2021-03-20 08:44:27 +00:00
> **[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/)
2021-03-19 07:35:25 +00:00
> nums
:
> : [1,2,1,3,2,5]
> : [3,5]
2021-07-23 13:18:15 +00:00
1 2
2021-03-19 07:35:25 +00:00
### HashSet
####
HashSet
####
2021-07-23 13:18:15 +00:00
Java Code:
2021-03-19 07:35:25 +00:00
```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);
2021-07-23 13:18:15 +00:00
//不存在则存入
} else {
set.add(x);
2021-03-19 07:35:25 +00:00
}
}
//存到数组里,然后返回
int[] arr = new int[2];
int i = 0;
for (int y : set) {
arr[i++] = y;
}
return arr;
}
}
```
2021-07-23 13:18:15 +00:00
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
```
2021-03-19 07:35:25 +00:00
###
####
2021-07-23 13:18:15 +00:00
> **a, b, a, b, c, d, e, f, e, f **
>
>
2021-03-19 07:35:25 +00:00
>
2021-07-23 13:18:15 +00:00
> Aa, a, b, b, c c
2021-03-19 07:35:25 +00:00
>
2021-07-23 13:18:15 +00:00
> Be, e, f, f, d d
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
cd cd A B
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
cd 1 1
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
001 100 = 101 1 1 1 0 A 1 B
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
c , d 0 101 001 100
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
a & 1 a 0 1 101 001 x & 001 x x & 100
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
0 101 001
2021-03-19 07:35:25 +00:00
2021-07-23 13:18:15 +00:00
x & (-x) 1
2021-03-19 07:35:25 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/分组位.25gbi25kv7c0.png)
####
2021-07-23 13:18:15 +00:00
Java Code:
2021-03-19 07:35:25 +00:00
```java
class Solution {
public int[] singleNumber(int[] nums) {
2021-07-23 13:18:15 +00:00
//小心溢出
long temp = 0;
2021-03-19 07:35:25 +00:00
//求出异或值
for (int x : nums) {
temp ^= x;
}
2021-07-23 13:18:15 +00:00
System.out.println(temp);
System.out.println(-temp);
2021-03-19 07:35:25 +00:00
//保留最右边的一个 1
2021-07-23 13:18:15 +00:00
long group = temp & (-temp);
2021-03-19 07:35:25 +00:00
int[] arr = new int[2];
for (int y : nums) {
2021-07-23 13:18:15 +00:00
//分组位为 0 的组,组内异或
2021-03-19 07:35:25 +00:00
if ((group & y) == 0) {
arr[0] ^= y;
2021-07-23 13:18:15 +00:00
//分组位为 1 的组,组内异或
2021-03-19 07:35:25 +00:00
} else {
arr[1] ^= y;
}
}
return arr;
}
}
```
2021-07-23 13:18:15 +00:00
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
```