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

290 lines
9.2 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>进入。
#### [137. II](https://leetcode-cn.com/problems/single-number-ii/)
>
1:
> : [2,2,3,2]
> : 3
2:
> : [0,1,0,1,0,1,99]
> : 99
###
####
OK
1. HashSet
2.SumSet \* 3 - SumNum/ 2 2 2
####
Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>();
long setsum = 0;
long numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
if (!set.contains(x)) {
//HashSet元素和
setsum += x;
}
set.add(x);
}
//返回只出现一次的数
return (int)((3 * setsum - numsum) / 2);
}
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_set<int> set;
long setsum = 0;
long numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
if (set.find(x) == set.end()) {
//HashSet内元素的和
setsum += x;
}
set.insert(x);
}
//返回值
return (3 * setsum - numsum) / 2;
}
};
```
JS Code:
```javascript
var singleNumber = function (nums) {
let set = new Set();
let setsum = 0;
let numsum = 0;
for (let x of nums) {
//所有元素的和
numsum += x;
if (!set.has(x)) {
setsum += x;
}
//HashSet内元素的和
set.add(x);
}
//返回值
return (3 * setsum - numsum) / 2;
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return (3 * sum(set(nums)) - sum(nums)) // 2
```
HashMap
###
####
3
![2](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/只出现一次的数字2.5p4wxbiegxc0.png)
3 1 1 3 3 +1 3 +1
####
Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int i = 0; i < 32; i++){
int count = 0;
for (int num: nums) {
//检查第 i 位是否为 1
if ((num >> i & 1) == 1) {
count++;
}
}
if (count % 3 != 0) {
// 将第 i 位设为 1
res = res | 1 << i;
}
}
return res;
}
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for(int i = 0; i < 32; i++){
int count = 0;
for (int num: nums) {
//检查第 i 位是否为 1
if ((num >> i & 1) == 1) {
count++;
}
}
if (count % 3 != 0) {
// 将第 i 位设为 1
res = res | 1 << i;
}
}
return res;
}
};
```
JS Code:
```javascript
var singleNumber = function (nums) {
let res = 0;
for (let i = 0; i < 32; i++) {
let count = 0;
for (let num of nums) {
//检查第 i 位是否为 1
if (((num >> i) & 1) == 1) {
count++;
}
}
if (count % 3 != 0) {
// 将第 i 位设为 1
res = res | (1 << i);
}
}
return res;
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = 0
for i in range(32):
count = 0
for num in nums:
# i 1
if (num >> i & 1) == 1:
count += 1
if count % 3 != 0:
# i 1
res = res | 1 << i
#
if (res >> 31 & 1) == 1:
res = ~(res ^ 4294967295)
return res
```
Go Code:
```go
func singleNumber(nums []int) int {
res := 0
// Go语言中int占32位以上
for i := 0; i < 64; i++ {
cnt := 0
for j := 0; j < len(nums); j++ {
if (nums[j] >> i & 1) == 1 {
cnt++
}
}
if cnt % 3 != 0{
res = res | 1 << i
}
}
return res
}
```
> **<<** **<<** 0
>
> **>>** **>> ** **>>**
a & 1 a | 1
> **&** 1, 1 0
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/只出现一次的数位运算且.vq3lcgv0rbk.png)
a & 1 1 1 0 **a & 1 a 1** a 1 a & 1 = 1 0 a
> **|** 1 1
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/或运算.6orep3gsrxc0.png)
** res 0 1** 1
> [@jaredliw](https://github.com/jaredliw)注:
>
> python
>
> ```python
> if (res >> 31 & 1) == 1:
> res = ~(res ^ 4294967295)
> ```
>
> int 32 1 0 python int res 32 -4
>
> > 11111111111111111111111111111100 1 = -4
>
> python
>
> > ...000000000000 11111111111111111111111111111100 0 0 = 4294967292
>
>
>
> res 32 4294967295 4294967295 32 1
>
> > ...000000000000 00000000000000000000000000000011 0= 3
>
>
>
> > ...111111111111 11111111111111111111111111111100 1 1= -4
>
> `(res >> n & 1) == 1` n 便 31 false true 33 34 1
>
> `res -= 2 ** 32` int