代码重构 【Github Actions】

This commit is contained in:
github-actions[bot]
2021-07-23 15:44:19 +00:00
parent c79cac3d9c
commit f671c90754
94 changed files with 1609 additions and 2111 deletions

View File

@@ -1,10 +1,8 @@
>
>
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/)
@@ -90,26 +88,26 @@ public:
JS Code:
```javascript
var singleNumber = function(nums) {
//特殊情况
if (nums.length === 1) {
return nums[0];
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;
}
//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;
}
map[x] += 1;
}
//遍历出出现次数为1的情况
for (let key in map) {
if (map[key] === 1) {
return key;
}
}
};
```
@@ -133,8 +131,6 @@ class Solution:
return y
```
### 排序搜索法
#### 解析
@@ -187,15 +183,15 @@ public:
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];
}
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];
}
return nums[nums.length - 1];
};
```
@@ -212,8 +208,6 @@ class Solution:
return nums[len(nums) - 1]
```
### HashSet
#### 解析
@@ -272,19 +266,19 @@ public:
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);
}
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;
}
return set.values().next().value;
};
```
@@ -306,8 +300,6 @@ class Solution:
return set_.pop()
```
###
#### 解析
@@ -335,7 +327,7 @@ class Solution {
break;
}
//相同时出栈
stack.pop();
stack.pop();
}
return stack.peek();
}
@@ -370,22 +362,22 @@ public:
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();
var singleNumber = function (nums) {
nums.sort();
let stack = [];
for (let x of nums) {
if (!stack.length) {
stack.push(x);
continue;
}
return stack[0];
//不同时直接跳出
if (stack[0] !== x) {
break;
}
//相同时出栈
stack.pop();
}
return stack[0];
};
```
@@ -412,17 +404,15 @@ class Solution:
return stack[-1]
```
### 求和法
#### 解析
这个方法也比较简单也是借助咱们的 HashSet 具体思路如下我们通过 HashSet 保存数组内的元素然后进行求和setsum那么得到的这个和则为去除掉重复元素的和我们也可以得到所有元素和numsum因为我们其他元素都出现两次仅有一个元素出现一次那我们通过 setsum * 2 - numsum 得到的元素则为出现一次的数
这个方法也比较简单也是借助咱们的 HashSet 具体思路如下我们通过 HashSet 保存数组内的元素然后进行求和setsum那么得到的这个和则为去除掉重复元素的和我们也可以得到所有元素和numsum因为我们其他元素都出现两次仅有一个元素出现一次那我们通过 setsum \* 2 - numsum 得到的元素则为出现一次的数
![求和解法](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/求和解法.2tds49a3vzq0.png)
上面我们的 SetSum * 2 - NumSum = z 也就是我们所求的值 是不是感觉很简单呀
上面我们的 SetSum \* 2 - NumSum = z 也就是我们所求的值 是不是感觉很简单呀
#### 题目代码
@@ -436,13 +426,13 @@ class Solution {
int numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
numsum += x;
if (!set.contains(x)) {
//HashSet内元素的和
setsum += x;
setsum += x;
}
set.add(x);
}
}
//返回值
return setsum * 2 - numsum;
}
@@ -460,13 +450,13 @@ public:
int numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
numsum += x;
if (set.find(x) == set.end()) {
//HashSet内元素的和
setsum += x;
setsum += x;
}
set.insert(x);
}
}
//返回值
return setsum * 2 - numsum;
}
@@ -476,21 +466,21 @@ public:
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);
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;
}
//返回值
return 2 * setsum - numsum;
//HashSet内元素的和
set.add(x);
}
//返回值
return 2 * setsum - numsum;
};
```
@@ -502,18 +492,16 @@ class Solution:
return 2 * sum(set(nums)) - sum(nums)
```
### 位运算
#### 解析
这个方法主要是借助咱们的位运算符 ^ 按位异或我们先来了解一下这个位运算符
> 按位异或XOR运算符^是双目运算符 其功能是参与运算的两数各对应的二进位相异或当两对应的二进位相异时结果为1相同时为0
> 按位异或XOR运算符^是双目运算符 其功能是参与运算的两数各对应的二进位相异或当两对应的二进位相异时结果为 1相同时为 0
> 任何数和0异或仍为本身a0 = a
> 任何数和本身异或为0aa = 0
> 任何数和 0 异或仍为本身a0 = a
> 任何数和本身异或 0aa = 0
> 异或运算满足交换律和结合律aba = (aa)b = 0b = b
@@ -533,7 +521,7 @@ class Solution {
public int singleNumber(int[] nums) {
int num = 0;
//异或
for (int x : nums) {
for (int x : nums) {
num ^= x;
}
return num;
@@ -560,9 +548,9 @@ public:
JS Code:
```javascript
var singleNumber = function(nums) {
//异或
return nums.reduce((num, x) => num ^= x);
var singleNumber = function (nums) {
//异或
return nums.reduce((num, x) => (num ^= x));
};
```
@@ -577,5 +565,4 @@ class Solution:
return reduce(lambda num, x: num ^ x, nums, 0)
```
本题一共介绍了6种解题方法肯定还有别的方法欢迎大家讨论大家可以在做题的时候一题多解这样能大大提高自己解题能力下面我们来看一下这些方法如何应用到其他题目上
本题一共介绍了 6 种解题方法肯定还有别的方法欢迎大家讨论大家可以在做题的时候一题多解这样能大大提高自己解题能力下面我们来看一下这些方法如何应用到其他题目上

View File

@@ -1,8 +1,8 @@
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
> 另外希望手机阅读的同学可以来我的 <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/)
@@ -28,7 +28,7 @@
1.通过遍历数组获取所有元素的和以及 HashSet 内元素的和
2.SumSet * 3 - SumNum/ 2 即可除以 2 是因为我们减去之后得到的是 2 倍的目标元素
2.SumSet \* 3 - SumNum/ 2 即可除以 2 是因为我们减去之后得到的是 2 倍的目标元素
这个题目中需要注意溢出的情况
@@ -41,7 +41,7 @@ class Solution {
public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>();
long setsum = 0;
long numsum = 0;
long numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
@@ -68,13 +68,13 @@ public:
long numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
numsum += x;
if (set.find(x) == set.end()) {
//HashSet内元素的和
setsum += x;
setsum += x;
}
set.insert(x);
}
}
//返回值
return (3 * setsum - numsum) / 2;
}
@@ -84,21 +84,21 @@ public:
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);
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;
}
//返回值
return (3 * setsum - numsum) / 2;
//HashSet内元素的和
set.add(x);
}
//返回值
return (3 * setsum - numsum) / 2;
};
```
@@ -176,22 +176,22 @@ public:
JS Code:
```javascript
var singleNumber = function(nums) {
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;
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;
return res;
};
```
@@ -216,32 +216,26 @@ class Solution:
return res
```
我们来解析一下我们的代码
> **<<** 左移运算符运算数的各二进位全部左移若干位 **<<** 右边的数字指定了移动的位数高位丢弃低位补0
> **<<** 左移运算符运算数的各二进位全部左移若干位 **<<** 右边的数字指定了移动的位数高位丢弃低位补 0
>
> **>>** 右移运算符**>> ** 左边的运算数的各二进位全部右移若干位**>>** 右边的数字指定了移动的位数
另外我们的代码中还包含了 a & 1 a | 1 这有什么作用呢继续看下图
> **&** 按位与运算符参与运算的两个值如果两个相应位都为1,则该位的结果为1否则为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
> **|** 按位或运算符只要对应的二个二进位有一个为 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 里的这两行
@@ -251,9 +245,9 @@ class Solution:
> res = ~(res ^ 4294967295)
> ```
>
> int 的的符号是由最高位这题用的是32的值决定1 就是负数0就是正数由于 python int 类型理论上是无限大的这题里的 res 都会被认定为是正数举个例子32位的 -4 是这样的
> int 的的符号是由最高位这题用的是 32 的值决定1 就是负数0 就是正数由于 python int 类型理论上是无限大的这题里的 res 都会被认定为是正数举个例子32 位的 -4 是这样的
>
> > 11111111111111111111111111111100 最高位是 1 = -4
> > 11111111111111111111111111111100 最高位是 1 = -4
>
> python 里的则是这样的
>
@@ -261,9 +255,9 @@ class Solution:
>
> 怎么办呢
>
> 我们可以先将 res 的后32位取反 4294967295 异或4294967295 的二进制是 32 1得到
> 我们可以先将 res 的后 32 位取反 4294967295 异或4294967295 的二进制是 32 1得到
>
> > ...000000000000 00000000000000000000000000000011最高位是0= 3
> > ...000000000000 00000000000000000000000000000011最高位是 0= 3
>
> 之后再用波浪号按位取反得到
>
@@ -272,4 +266,3 @@ class Solution:
> 大家可以自行验证看看`(res >> n & 1) == 1` n 随便填个大于 31 的数字之前是 false之后就变成 true 代表第 33 34 都转成 1
>
> 虽然说这种方法有一种脱裤子放屁的感觉 而且`res -= 2 ** 32` 也能办到但由于涉及到 int 存储的问题我省略了许多大家自行度娘哈我觉得还是有必要知道的

View File

@@ -1,8 +1,8 @@
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
> 另外希望手机阅读的同学可以来我的 <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/)
@@ -42,7 +42,7 @@ class Solution {
int[] arr = new int[2];
int i = 0;
for (int y : set) {
arr[i++] = y;
arr[i++] = y;
}
return arr;
}
@@ -78,23 +78,23 @@ public:
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);
}
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;
}
//存到数组里,然后返回
let arr = [];
for (let y of set) {
arr.push(y);
}
return arr;
};
```
@@ -118,8 +118,6 @@ class Solution:
return arr
```
### 位运算
#### 解析
@@ -132,13 +130,13 @@ class Solution:
>
> 分组后
>
> A组a, a, b, b, c 异或得到 c
> A a, a, b, b, c 异或得到 c
>
> B组e, e, f, f, d 异或得到 d
> B e, e, f, f, d 异或得到 d
原理懂了那么我们应该依据什么规则对其进行分类呢
cd 两个不同的数那么二进制上必定有一位是不同的那么我们就可以根据这一位分组位来将 cd 分到两个组中数组中的其他元素要么在 A 组中要么在 B 组中
cd 两个不同的数那么二进制上必定有一位是不同的那么我们就可以根据这一位分组位来将 cd 分到两个组中数组中的其他元素要么在 A 组中要么在 B 组中
我们应该怎么得到分组位呢
@@ -148,7 +146,7 @@ cd 两个不同的数,那么二进制上必定有一位是不同的,那
那么我们应该怎么借助分组位进行分组呢
我们处理 c , d 的异或值可以仅保留异或值的分组位其余位变为 0 例如 101 变成 001或 100
我们处理 c , d 的异或值可以仅保留异或值的分组位其余位变为 0 例如 101 变成 001 100
为什么要这么做呢在第二题提到我们可以根据 a & 1 来判断 a 的最后一位为 0 还是为 1所以我们将 101 变成 001 之后然后数组内的元素 x & 001 即可对 x 进行分组 同样也可以 x & 100 进行分组
@@ -176,7 +174,7 @@ class Solution {
//保留最右边的一个 1
long group = temp & (-temp);
int[] arr = new int[2];
for (int y : nums) {
for (int y : nums) {
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
@@ -205,7 +203,7 @@ public:
//保留最右边的一个 1
int group = temp & (-temp);
vector<int> arr(2, 0);
for (int y : nums) {
for (int y : nums) {
//分组位为 0 的组,组内异或
if ((group & y) == 0) {
arr[0] ^= y;
@@ -222,25 +220,25 @@ public:
JS Code:
```javascript
var singleNumber = function(nums) {
let temp = 0;
//求出异或值
for (let x of nums) {
temp ^= x;
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;
}
//保留最右边的一个 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;
}
return arr;
};
```
@@ -265,4 +263,3 @@ class Solution:
arr[1] ^= y
return arr
```