添加py,js和cpp,去除多余代码,添加注释

pull/38/head
jaredliw 2021-07-23 18:16:12 +08:00
parent ae5cd15279
commit e761b70650
1 changed files with 185 additions and 25 deletions

View File

@ -28,88 +28,248 @@
1. HashSet 1. HashSet
2.SumSet * 3 - SumNum/ 2 2 2 2.SumSet * 3 - SumNum/ 2 2 2
#### ####
Java Code:
```java ```java
class Solution { class Solution {
public int singleNumber(int[] nums) { public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>(); HashSet<Integer> set = new HashSet<>();
long sumset = 0; long setsum = 0;
long sumnum = 0; long numsum = 0;
for (int x : nums) { for (int x : nums) {
//所有元素的和 //所有元素的和
sumnum += x; numsum += x;
if (set.contains(x)) { if (!set.contains(x)) {
continue; //HashSet元素和
} setsum += x;
//HashSet元素和 }
sumset += x;
set.add(x); set.add(x);
} }
//返回只出现一次的数 //返回只出现一次的数
return (int)((3 * sumset - sumnum) / 2); 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 HashMap
### ###
#### ####
3
![2](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/只出现一次的数字2.5p4wxbiegxc0.png) ![2](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/只出现一次的数字2.5p4wxbiegxc0.png)
3 1 1 2 3 3 +1 3 +1 3 1 1 3 3 +1 3 +1
#### ####
Java Code:
```java ```java
class Solution { class Solution {
public int singleNumber(int[] nums) { public int singleNumber(int[] nums) {
int res = 0; int res = 0;
for(int i = 0; i < 32; i++){ for(int i = 0; i < 32; i++){
int count = 0; int count = 0;
for (int j = 0; j < nums.length; j++) { for (int num: nums) {
//先将数右移,并求出最后一位为 1 的个数 //检查第 i 位是否为 1
if ((nums[j] >> i & 1) == 1) { if ((num >> i & 1) == 1) {
count++; count++;
} }
} }
//找到某一位取余为 1 的数,并左移,为了将这一位循环结束后移至原位
if (count % 3 != 0) { if (count % 3 != 0) {
// 将第 i 位设为 1
res = res | 1 << i; res = res | 1 << i;
} }
} }
return res; return res;
} }
} }
``` ```
C++ Code:
> **<<** **<<** 0 ```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
```
> **<<** **<<** 0
> >
> **>>** ">>"**>>** > **>>** **>> ** **>>**
a & 1 a | 1 a & 1 a | 1
> **&** ,1,1,0 > **&** 1,10
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/只出现一次的数位运算且.vq3lcgv0rbk.png) ![](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 a & 1 1 1 0 **a & 1 a 1** a 1 a & 1 = 1 0 a
> **|** 11 > **|** 11
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/或运算.6orep3gsrxc0.png) ![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/或运算.6orep3gsrxc0.png)
res 0 1 1 ** res 0 1** 1
> [@jaredliw](https://github.com/jaredliw)注:
>
> python
>
> ```python
> if (res >> 31 & 1) == 1:
> res = ~(res ^ 4294967295)
> ```
>
> int 321 0 python int res 32 -4
>
> > 11111111111111111111111111111100 1 = -4
>
> python
>
> > ...000000000000 11111111111111111111111111111100 0 0 = 4294967292
>
>
>
> res 32 4294967295 4294967295 32 1
>
> > ...000000000000 000000000000000000000000000000110= 3
>
>
>
> > ...111111111111 11111111111111111111111111111100 1 1= -4
>
> `(res >> n & 1) == 1` n 便 31 false true 33 34 1
>
> `res -= 2 ** 32` int