mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 21:01:08 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
|
||||
### [75 颜色分类](https://leetcode-cn.com/problems/sort-colors/)
|
||||
### [75 颜色分类](https://leetcode-cn.com/problems/sort-colors/)
|
||||
|
||||
题目描述:
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
|
||||
**做题思路**
|
||||
|
||||
这个题目我们使用 Arrays.sort() 解决,哈哈,但是那样太无聊啦,题目含义就是让我们将所有的 0 放在前面,2放在后面,1 放在中间,是不是和我们上面说的荷兰国旗问题一样。我们仅仅将 1 做为 pivot 值。
|
||||
这个题目我们使用 Arrays.sort() 解决,哈哈,但是那样太无聊啦,题目含义就是让我们将所有的 0 放在前面,2 放在后面,1 放在中间,是不是和我们上面说的荷兰国旗问题一样。我们仅仅将 1 做为 pivot 值。
|
||||
|
||||
下面我们直接看代码吧,和三向切分基本一致。
|
||||
|
||||
@@ -48,7 +48,7 @@ class Solution {
|
||||
//这里和三向切分不完全一致
|
||||
int i = left;
|
||||
int right = len-1;
|
||||
|
||||
|
||||
while (i <= right) {
|
||||
if (nums[i] == 2) {
|
||||
swap(nums,i,right--);
|
||||
@@ -57,7 +57,7 @@ class Solution {
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
@@ -72,7 +72,7 @@ Python3 Code:
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def sortColors(self, nums: List[int]):
|
||||
def sortColors(self, nums: List[int]):
|
||||
leng = len(nums)
|
||||
left = 0
|
||||
# 这里和三向切分不完全一致
|
||||
@@ -89,7 +89,7 @@ class Solution:
|
||||
else:
|
||||
i += 1
|
||||
return nums
|
||||
|
||||
|
||||
def swap(self, nums: List[int], i: int, j: int):
|
||||
temp = nums[i]
|
||||
nums[i] = nums[j]
|
||||
@@ -112,7 +112,7 @@ public:
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -153,7 +153,7 @@ class Solution {
|
||||
|
||||
例如:[0,0,0,1,1,1,2,2,2]
|
||||
|
||||
此时我们完全符合情况,不需要交换元素,但是按照我们上面的代码,0,2 的每个元素会和自己进行交换,所以这里我们可以根据 i 和 left 的值是否相等来决定是否需要交换,大家可以自己写一下。
|
||||
此时我们完全符合情况,不需要交换元素,但是按照我们上面的代码,0,2 的每个元素会和自己进行交换,所以这里我们可以根据 i 和 left 的值是否相等来决定是否需要交换,大家可以自己写一下。
|
||||
|
||||
下面我们看一下另外一种写法
|
||||
|
||||
@@ -177,7 +177,7 @@ class Solution {
|
||||
int len = nums.length;
|
||||
int right = len - 1;
|
||||
for (int i = 0; i <= right; ++i) {
|
||||
if (nums[i] == 0) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums,i,left);
|
||||
left++;
|
||||
}
|
||||
@@ -190,7 +190,7 @@ class Solution {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public void swap (int[] nums,int i, int j) {
|
||||
int temp = nums[i];
|
||||
@@ -220,7 +220,7 @@ class Solution:
|
||||
# 如果不等于 1 则需要继续判断,所以不移动 i 指针,i--
|
||||
if nums[i] != 1:
|
||||
i -= 1
|
||||
i += 1
|
||||
i += 1
|
||||
return nums
|
||||
|
||||
def swap(self, nums: List[int], i: int, j: int):
|
||||
@@ -238,7 +238,7 @@ public:
|
||||
int left = 0, len = nums.size();
|
||||
int right = len - 1;
|
||||
for (int i = 0; i <= right; ++i) {
|
||||
if (nums[i] == 0) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums[i],nums[left++]);
|
||||
}
|
||||
if (nums[i] == 2) {
|
||||
@@ -265,7 +265,7 @@ class Solution {
|
||||
//nums.swapAt(i, left) 直接调用系统方法
|
||||
self.swap(&nums, i, left) // 保持风格统一走自定义交换
|
||||
left += 1
|
||||
}
|
||||
}
|
||||
if nums[i] == 2 {
|
||||
//nums.swapAt(i, right) 直接调用系统方法
|
||||
self.swap(&nums, i, right) // 保持风格统一走自定义交换
|
||||
@@ -285,4 +285,4 @@ class Solution {
|
||||
nums[j] = temp
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user