代码重构 【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,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>进入。
### 快速排序
@@ -50,23 +50,23 @@ Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort (int[] nums, int low, int high) {
if (low < high) {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,high);
}
}
}
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
while (low < high) {
//移动hight指针
@@ -84,7 +84,7 @@ class Solution {
//基准数放到合适的位置
nums[low] = pivot;
return low;
}
}
}
```
@@ -102,11 +102,11 @@ class Solution {
```java
class Solution {
public int[] sortArray (int[] nums) {
public int[] sortArray (int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort (int[] nums, int low, int high) {
@@ -115,30 +115,30 @@ class Solution {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,high);
}
}
}
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
int start = low;
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
swap(nums, low, high);
}
//基准值归位
swap(nums,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
```
@@ -150,7 +150,7 @@ class Solution {
**快速排序的空间复杂度分析**
快速排序主要时递归造成的栈空间的使用最好情况时其空间复杂度为O (logn),对应递归树的深度最坏情况时则需要 n-1 次递归调用此时空间复杂度为O(n).
快速排序主要时递归造成的栈空间的使用最好情况时其空间复杂度为 O (logn),对应递归树的深度最坏情况时则需要 n-1 次递归调用此时空间复杂度为 O(n).
**快速排序的稳定性分析**
@@ -158,7 +158,7 @@ class Solution {
![稳定性](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218165440.17ovoc8246gw.png)
此时无论使用哪一种方法第一次排序后黄色的 1 都会跑到红色 1 的前面所以快速排序是不稳定的排序算法
此时无论使用哪一种方法第一次排序后黄色的 1 都会跑到红色 1 的前面所以快速排序是不稳定的排序算法
![性能分析](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218170306.6oe0mbb5gr40.png)
@@ -170,7 +170,7 @@ class Solution {
```java
class Solution {
public int[] sortArray(int[] nums) {
Stack<Integer> stack = new Stack<>();
stack.push(nums.length - 1);
@@ -191,25 +191,25 @@ class Solution {
}
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
int start = low;
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
swap(nums, low, high);
}
swap(nums,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
```
@@ -231,7 +231,7 @@ class Solution {
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
if (nums[mid] > nums[low]) swap(nums,mid,low);
```
其含义就是让我们将中间元素放到 nums[low] 位置做为基准值最大值放到 nums[high],最小值放到 nums[mid], [4,2,3] 经过上面代码处理后则变成了 [3,2,4].此时我们选取 3 做为基准值这样也就避免掉了选取最大或最小值做为基准值的情况
@@ -240,7 +240,7 @@ class Solution {
```java
class Solution {
public int[] sortArray(int[] nums) {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
@@ -249,7 +249,7 @@ class Solution {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,high);
}
}
}
public int partition (int[] nums, int low, int high) {
@@ -257,24 +257,24 @@ class Solution {
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
if (nums[mid] > nums[low]) swap(nums,mid,low);
//下面和之前一样,仅仅是多了上面几行代码
int pivot = nums[low];
int start = low;
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
swap(nums, low, high);
}
swap(nums,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
```
@@ -287,43 +287,43 @@ class Solution {
```java
class Solution {
private static final int INSERTION_SORT_MAX_LENGTH = 7;
public int[] sortArray(int[] nums) {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort (int[] nums, int low, int high) {
if (high - low <= INSERTION_SORT_MAX_LENGTH) {
insertSort(nums,low,high);
return;
}
}
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,high);
quickSort(nums,index+1,high);
}
public int partition (int[] nums, int low, int high) {
//三数取中,大家也可以使用其他方法
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
if (nums[mid] > nums[low]) swap(nums,mid,low);
int pivot = nums[low];
int start = low;
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
swap(nums, low, high);
}
swap(nums,start,low);
return low;
}
}
public void insertSort (int[] nums, int low, int high) {
for (int i = low+1; i <= high; ++i) {
int temp = nums[i];
int j;
@@ -331,18 +331,18 @@ class Solution {
if (temp < nums[j]) {
nums[j+1] = nums[j];
continue;
}
}
break;
}
nums[j+1] = temp;
}
}
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
```
@@ -362,7 +362,7 @@ class Solution {
![三向切分](https://img-blog.csdnimg.cn/20210317190456320.gif#pic_center)
我们来剖析一下视频其实原理很简单我们利用探路指针也就是 i遇到比 pivot 大的元素则和 right 指针进行交换此时 right 指向的元素肯定比 pivot right--但是此时我们的 nums[i] 指向的元素并不知道情况所以我们的 i 指针不动如果此时 nums[i] < pivot 则与 left 指针交换注意此时我们的 left 指向的值肯定是 等于 povit的所以交换后我们要 left++,i++, nums[i] == pivot 仅需要 i++ 即可继续判断下一个元素 我们也可以借助这个思想来解决经典的荷兰国旗问题
我们来剖析一下视频其实原理很简单我们利用探路指针也就是 i遇到比 pivot 大的元素则和 right 指针进行交换此时 right 指向的元素肯定比 pivot right--但是此时我们的 nums[i] 指向的元素并不知道情况所以我们的 i 指针不动如果此时 nums[i] < pivot 则与 left 指针交换注意此时我们的 left 指向的值肯定是 等于 povit 所以交换后我们要 left++,i++, nums[i] == pivot 仅需要 i++ 即可继续判断下一个元素 我们也可以借助这个思想来解决经典的荷兰国旗问题
好啦我们下面直接看代码吧
@@ -408,7 +408,7 @@ class Solution {
quickSort(nums,right+1,high);
}
public void insertSort (int[] nums, int low, int high) {
for (int i = low+1; i <= high; ++i) {
int temp = nums[i];
int j;
@@ -416,12 +416,12 @@ class Solution {
if (temp < nums[j]) {
nums[j+1] = nums[j];
continue;
}
}
break;
}
nums[j+1] = temp;
}
}
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
@@ -432,7 +432,7 @@ class Solution {
Python Code:
```python
```python
from typing import List
class Solution:
INSERTION_SORT_MAX_LENGTH = 7
@@ -489,6 +489,4 @@ class Solution:
nums[j] = temp
```
好啦一些常用的优化方法都整理出来啦还有一些其他的优化算法九数取中优化递归操作等就不在这里进行描述啦感兴趣的可以自己看一下好啦这期的文章就到这里啦我们下期见拜了个拜