添加py,js和cpp,解决java溢出

pull/38/head
jaredliw 2021-07-23 21:18:15 +08:00
parent e761b70650
commit 9103e4379f
1 changed files with 174 additions and 19 deletions

View File

@ -13,7 +13,7 @@
> : [1,2,1,3,2,5]
> : [3,5]
1 1 2 1
1 2
### HashSet
@ -23,6 +23,8 @@
####
Java Code:
```java
class Solution {
public int[] singleNumber(int[] nums) {
@ -31,10 +33,10 @@ class Solution {
//存在的则移除
if (set.contains(x)) {
set.remove(x);
continue;
//不存在则存入
} else {
set.add(x);
}
//不存在存入
set.add(x);
}
//存到数组里,然后返回
int[] arr = new int[2];
@ -47,6 +49,77 @@ class Solution {
}
```
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
```
###
####
@ -55,53 +128,59 @@ class Solution {
> **a,b,a,b,c,d,e,f,e,f **
> **a, b, a, b, c, d, e, f, e, f **
>
> Aa, a , b, b, c c
>
>
> Be, e, f, f, d d
> Aa, a, b, b, c c
>
> Be, e, f, f, d d
c , d () c , d A B
cd cd A B
c , d 1 , 1
cd 1 1
001 100 = 101 1 1 1 0 A 1 B
001 100 = 101 1 1 1 0 A 1 B
c , d 0 101 001 100
c , d 0 101 001 100
a & 1 a 0 1 101 001 x & 001 x x & 100 .
a & 1 a 0 1 101 001 x & 001 x x & 100
0 101 001
0 101 001
x & (-x) 1
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) {
int temp = 0;
//小心溢出
long temp = 0;
//求出异或值
for (int x : nums) {
temp ^= x;
}
System.out.println(temp);
System.out.println(-temp);
//保留最右边的一个 1
int group = temp & (-temp);
System.out.println(group);
long group = temp & (-temp);
int[] arr = new int[2];
for (int y : nums) {
//分组位为0的组组内异或
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
//分组位为 1 的组,组内异或
//分组位为 1 的组,组内异或
} else {
arr[1] ^= y;
}
@ -111,3 +190,79 @@ class Solution {
}
```
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
```