mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-02 14:01:34 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -261,7 +261,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -304,4 +303,3 @@ func max(a, b int) int {
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -203,7 +203,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -218,4 +217,3 @@ func twoSum(nums []int, target int) []int {
|
||||
return []int{}
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -45,7 +45,7 @@ class Solution {
|
||||
if (map.containsKey(nums[i])) {
|
||||
//判断是否小于K,如果小于等于则直接返回
|
||||
int abs = Math.abs(i - map.get(nums[i]));
|
||||
if (abs <= k) return true;//小于等于则返回
|
||||
if (abs <= k) return true;//小于等于则返回
|
||||
}
|
||||
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||
map.put(nums[i],i);
|
||||
@@ -72,7 +72,7 @@ class Solution:
|
||||
# 判断是否小于K,如果小于等于则直接返回
|
||||
a = abs(i - m[nums[i]])
|
||||
if a <= k:
|
||||
return True# 小于等于则返回
|
||||
return True# 小于等于则返回
|
||||
# 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||
m[nums[i]] = i
|
||||
return False
|
||||
@@ -243,4 +243,3 @@ func containsNearbyDuplicate(nums []int, k int) bool {
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -183,7 +183,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -203,4 +202,3 @@ func removeElement(nums []int, val int) int {
|
||||
return i
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -274,7 +274,6 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -297,4 +296,3 @@ func firstMissingPositive(nums []int) int {
|
||||
return length + 1
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -22,7 +22,7 @@
|
||||
|
||||
下面我们通过一个视频模拟代码执行步骤大家一下就能搞懂了。
|
||||
|
||||

|
||||

|
||||
|
||||
下面我们直接看代码吧
|
||||
|
||||
@@ -231,4 +231,3 @@ func max(a, b int) int {
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -181,7 +181,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -208,7 +207,7 @@ func spiralOrder(matrix [][]int) []int {
|
||||
}
|
||||
down--
|
||||
if top > down { break }
|
||||
|
||||
|
||||
for i := down; i >= top; i-- {
|
||||
res = append(res, matrix[i][left])
|
||||
}
|
||||
@@ -218,4 +217,3 @@ func spiralOrder(matrix [][]int) []int {
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -236,4 +236,3 @@ func subarraySum(nums []int, k int) int {
|
||||
return cnt
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -361,7 +361,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -394,7 +393,7 @@ func generateMatrix(n int) [][]int {
|
||||
}
|
||||
buttom--
|
||||
if num > size { break }
|
||||
|
||||
|
||||
for i := buttom; i >= top; i-- {
|
||||
res[i][left] = num
|
||||
num++
|
||||
@@ -405,4 +404,3 @@ func generateMatrix(n int) [][]int {
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <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>进入。
|
||||
|
||||
#### [66. 加一](https://leetcode-cn.com/problems/plus-one/)
|
||||
|
||||
@@ -56,10 +56,10 @@ class Solution {
|
||||
if (digits[i] != 0) {
|
||||
return digits;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//第三种情况,因为数组初始化每一位都为0,我们只需将首位设为1即可
|
||||
int[] arr = new int[len+1];
|
||||
int[] arr = new int[len+1];
|
||||
arr[0] = 1;
|
||||
return arr;
|
||||
}
|
||||
@@ -138,4 +138,3 @@ func plusOne(digits []int) []int {
|
||||
return digits
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -173,8 +173,6 @@ func sortColors(nums []int) {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
另外我们看这段代码,有什么问题呢?那就是我们即使完全符合时,仍会交换元素,这样会大大降低我们的效率。
|
||||
|
||||
例如:[0,0,0,1,1,1,2,2,2]
|
||||
@@ -203,7 +201,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++;
|
||||
}
|
||||
@@ -216,7 +214,7 @@ class Solution {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public void swap (int[] nums,int i, int j) {
|
||||
int temp = nums[i];
|
||||
@@ -246,7 +244,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):
|
||||
@@ -264,7 +262,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) {
|
||||
@@ -291,7 +289,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) // 保持风格统一走自定义交换
|
||||
@@ -336,4 +334,3 @@ func sortColors(nums []int) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <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>进入。
|
||||
|
||||
#### [剑指 Offer 03. 数组中重复的数字](https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
输入:
|
||||
[2, 3, 1, 0, 2, 5, 3]
|
||||
输出:2 或 3
|
||||
输出:2 或 3
|
||||
|
||||
#### **HashSet**
|
||||
|
||||
|
@@ -120,7 +120,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@@ -150,4 +149,3 @@ func min(a, b int) int {
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user