添加py,js和c++,删除多于代码

This commit is contained in:
jaredliw 2021-07-22 23:12:26 +08:00
parent bf547801e9
commit ae5cd15279

View File

@ -1,3 +1,5 @@
>
>
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** 备注 github + 题目 + 问题 向我反馈
>
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
@ -30,6 +32,8 @@
#### 题目代码
Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
@ -53,6 +57,64 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
//特殊情况
if (nums.size() == 1) {
return nums[0];
}
//HashMap
unordered_map<int,int> map;
//将其存入哈希表中含义为若该元素不存在则存入表中并计数为1若已经存在获取次数并加1
for (int x : nums) {
if (map.find(x) == map.end()) {
map.insert({x, 0});
}
map[x] += 1;
}
//遍历出出现次数为1的情况
for (int y : nums) {
if(map.at(y) == 1){
return y;
}
}
return -1;
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
//特殊情况
if (nums.length === 1) {
return nums[0];
}
//HashMap
map = {};
//将其存入哈希表中含义为若该元素不存在则存入表中并计数为1若已经存在获取次数并加1
for (let x of nums) {
if (!(x in map)) {
map[x] = 0;
}
map[x] += 1;
}
//遍历出出现次数为1的情况
for (let key in map) {
if (map[key] === 1) {
return key;
}
}
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
@ -104,13 +166,44 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
//排序
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size() - 1; i += 2){
if (nums[i] != nums[i - 1]){
return nums[i - 1];
}
}
return nums[nums.size() - 1];
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
// 排序
nums.sort();
for (let i = 1; i < nums.length - 1; i += 2) {
if (nums[i] != nums[i - 1]) {
return nums[i - 1];
}
}
return nums[nums.length - 1];
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
# 排序
nums.sort()
for i in range(1, len(nums), 2):
@ -136,18 +229,14 @@ Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1){
return nums[0];
}
HashSet<Integer> set = new HashSet<>();
//循环遍历
for (int x : nums){
//已经存在则去除
if(set.contains(x)){
set.remove(x);
}
//否则存入
else{
} else {
set.add(x);
}
}
@ -157,13 +246,53 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
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);
}
}
//返回仅剩的一个元素
return *set.begin();
}
};
```
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);
}
}
return set.values().next().value;
};
```
Python Code:
```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]
set_ = set()
# 循环遍历
for x in nums:
@ -189,12 +318,11 @@ class Solution:
#### 题目代码
Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
Arrays.sort(nums);
Stack<Integer> stack = new Stack<>();
for (int x : nums){
@ -214,6 +342,53 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
stack<int> stack;
for (int x: nums) {
if (stack.empty()) {
stack.push(x);
continue;
}
//不同时直接跳出
if (stack.top() != x) {
break;
}
//相同时出栈
stack.pop();
}
return stack.top();
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
nums.sort();
let stack = [];
for (let x of nums) {
if (!stack.length) {
stack.push(x);
continue;
}
//不同时直接跳出
if (stack[0] !== x) {
break;
}
//相同时出栈
stack.pop();
}
return stack[0];
};
```
Python Code:
```python
@ -229,8 +404,10 @@ class Solution:
if not stack:
stack.append(x)
continue
# 不同时直接跳出
if stack[-1] != x:
break
# 相同时出栈
stack.pop()
return stack[-1]
```
@ -249,12 +426,11 @@ class Solution:
#### 题目代码
Java Code:
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1){
return nums[0];
}
HashSet<Integer> set = new HashSet<>();
int setsum = 0;
int numsum = 0;
@ -273,6 +449,51 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_set<int> set;
int setsum = 0;
int numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
if (set.find(x) == set.end()) {
//HashSet内元素的和
setsum += x;
}
set.insert(x);
}
//返回值
return setsum * 2 - numsum;
}
};
```
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 2 * setsum - numsum;
};
```
Python Code:
```python
@ -289,7 +510,7 @@ class Solution:
这个方法主要是借助咱们的位运算符 ^ 按位异或我们先来了解一下这个位运算符
> 按位异或(XOR)运算符^是双目运算符 其功能是参与运算的两数各对应的二进位相异或当两对应的二进位相异时结果为1相同时为0
> 按位异或XOR运算符^是双目运算符 其功能是参与运算的两数各对应的二进位相异或当两对应的二进位相异时结果为1相同时为0
> 任何数和0异或仍为本身a0 = a
> 任何数和本身异或为0aa = 0
@ -320,6 +541,31 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
int num = 0;
//异或
for (vector<int>::iterator x = nums.begin(); x != nums.end(); x++) {
num ^= *x;
}
return num;
}
};
```
JS Code:
```javascript
var singleNumber = function(nums) {
//异或
return nums.reduce((num, x) => num ^= x);
};
```
Python Code:
```python
@ -327,7 +573,9 @@ from functools import reduce
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(lambda num, x: int(num) ^ int(x), nums)
# 异或
return reduce(lambda num, x: num ^ x, nums, 0)
```
本题一共介绍了6种解题方法肯定还有别的方法欢迎大家讨论大家可以在做题的时候一题多解这样能大大提高自己解题能力下面我们来看一下这些方法如何应用到其他题目上
本题一共介绍了6种解题方法肯定还有别的方法欢迎大家讨论大家可以在做题的时候一题多解这样能大大提高自己解题能力下面我们来看一下这些方法如何应用到其他题目上