mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 04:46:17 +00:00
代码重构 【Github Actions】
This commit is contained in:
parent
5a5325b0c8
commit
a8b66cd5ae
@ -157,4 +157,4 @@ func search(nums []int, target int) 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>进入。
|
||||||
|
|
||||||
#### [35. 搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)
|
#### [35. 搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)
|
||||||
|
|
||||||
@ -52,10 +52,10 @@ class Solution {
|
|||||||
//查询成功
|
//查询成功
|
||||||
if (target == nums[mid]) {
|
if (target == nums[mid]) {
|
||||||
return mid;
|
return mid;
|
||||||
//右区间
|
//右区间
|
||||||
} else if (nums[mid] < target) {
|
} else if (nums[mid] < target) {
|
||||||
left = mid + 1;
|
left = mid + 1;
|
||||||
//左区间
|
//左区间
|
||||||
} else if (nums[mid] > target) {
|
} else if (nums[mid] > target) {
|
||||||
right = mid - 1;
|
right = mid - 1;
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ class Solution {
|
|||||||
TreeNode cur = new TreeNode(-1);
|
TreeNode cur = new TreeNode(-1);
|
||||||
cur = root;
|
cur = root;
|
||||||
Stack<TreeNode> stack = new Stack<>();
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
while (!stack.isEmpty() || cur != null) {
|
while (!stack.isEmpty() || cur != null) {
|
||||||
//找到当前应该遍历的那个节点
|
//找到当前应该遍历的那个节点
|
||||||
while (cur != null) {
|
while (cur != null) {
|
||||||
stack.push(cur);
|
stack.push(cur);
|
||||||
@ -47,7 +47,7 @@ class Solution {
|
|||||||
cur = temp.right;
|
cur = temp.right;
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -104,4 +104,4 @@ func inorderTraversal(root *TreeNode) []int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
###
|
###
|
||||||
|
@ -49,10 +49,10 @@ class Solution {
|
|||||||
public List<Integer> preorderTraversal(TreeNode root) {
|
public List<Integer> preorderTraversal(TreeNode root) {
|
||||||
List<Integer> list = new ArrayList<>();
|
List<Integer> list = new ArrayList<>();
|
||||||
Stack<TreeNode> stack = new Stack<>();
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
if (root == null) return list;
|
if (root == null) return list;
|
||||||
stack.push(root);
|
stack.push(root);
|
||||||
while (!stack.isEmpty()) {
|
while (!stack.isEmpty()) {
|
||||||
TreeNode temp = stack.pop();
|
TreeNode temp = stack.pop();
|
||||||
if (temp.right != null) {
|
if (temp.right != null) {
|
||||||
stack.push(temp.right);
|
stack.push(temp.right);
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
我们知道后序遍历的顺序是,` 对于树中的某节点, 先遍历该节点的左子树, 再遍历其右子树, 最后遍历该节点`。
|
我们知道后序遍历的顺序是,` 对于树中的某节点, 先遍历该节点的左子树, 再遍历其右子树, 最后遍历该节点`。
|
||||||
|
|
||||||
那么我们如何利用栈来解决呢?
|
那么我们如何利用栈来解决呢?
|
||||||
|
|
||||||
我们直接来看动画,看动画之前,但是我们`需要带着问题看动画`,问题搞懂之后也就搞定了后序遍历。
|
我们直接来看动画,看动画之前,但是我们`需要带着问题看动画`,问题搞懂之后也就搞定了后序遍历。
|
||||||
|
|
||||||
|
@ -80,3 +80,4 @@ func dailyTemperatures(temperatures []int) []int {
|
|||||||
}
|
}
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
@ -111,7 +111,7 @@ func (mq *MaxQueue) Is_empty() bool {
|
|||||||
return mq.size == 0
|
return mq.size == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Max_value 取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
|
// Max_value 取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
|
||||||
func (mq *MaxQueue) Max_value() int {
|
func (mq *MaxQueue) Max_value() int {
|
||||||
if mq.Is_empty() { return -1 }
|
if mq.Is_empty() { return -1 }
|
||||||
return mq.deq[0]
|
return mq.deq[0]
|
||||||
|
@ -116,4 +116,5 @@ func (m *MinStack) GetMin() int {
|
|||||||
return m.minStk[len(m.minStk) - 1]
|
return m.minStk[len(m.minStk) - 1]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
###
|
###
|
||||||
|
@ -261,7 +261,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -304,4 +303,3 @@ func max(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -203,7 +203,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -218,4 +217,3 @@ func twoSum(nums []int, target int) []int {
|
|||||||
return []int{}
|
return []int{}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ class Solution {
|
|||||||
if (map.containsKey(nums[i])) {
|
if (map.containsKey(nums[i])) {
|
||||||
//判断是否小于K,如果小于等于则直接返回
|
//判断是否小于K,如果小于等于则直接返回
|
||||||
int abs = Math.abs(i - map.get(nums[i]));
|
int abs = Math.abs(i - map.get(nums[i]));
|
||||||
if (abs <= k) return true;//小于等于则返回
|
if (abs <= k) return true;//小于等于则返回
|
||||||
}
|
}
|
||||||
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||||
map.put(nums[i],i);
|
map.put(nums[i],i);
|
||||||
@ -72,7 +72,7 @@ class Solution:
|
|||||||
# 判断是否小于K,如果小于等于则直接返回
|
# 判断是否小于K,如果小于等于则直接返回
|
||||||
a = abs(i - m[nums[i]])
|
a = abs(i - m[nums[i]])
|
||||||
if a <= k:
|
if a <= k:
|
||||||
return True# 小于等于则返回
|
return True# 小于等于则返回
|
||||||
# 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
# 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||||
m[nums[i]] = i
|
m[nums[i]] = i
|
||||||
return False
|
return False
|
||||||
@ -243,4 +243,3 @@ func containsNearbyDuplicate(nums []int, k int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -183,7 +183,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -203,4 +202,3 @@ func removeElement(nums []int, val int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -274,7 +274,6 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -297,4 +296,3 @@ func firstMissingPositive(nums []int) int {
|
|||||||
return length + 1
|
return length + 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
下面我们通过一个视频模拟代码执行步骤大家一下就能搞懂了。
|
下面我们通过一个视频模拟代码执行步骤大家一下就能搞懂了。
|
||||||
|
|
||||||
![leetcode485最长连续1的个数](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/leetcode485最长连续1的个数.7avzcthkit80.gif)
|
![leetcode485最长连续1的个数](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/leetcode485最长连续1的个数.7avzcthkit80.gif)
|
||||||
|
|
||||||
下面我们直接看代码吧
|
下面我们直接看代码吧
|
||||||
|
|
||||||
@ -231,4 +231,3 @@ func max(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -181,7 +181,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -208,7 +207,7 @@ func spiralOrder(matrix [][]int) []int {
|
|||||||
}
|
}
|
||||||
down--
|
down--
|
||||||
if top > down { break }
|
if top > down { break }
|
||||||
|
|
||||||
for i := down; i >= top; i-- {
|
for i := down; i >= top; i-- {
|
||||||
res = append(res, matrix[i][left])
|
res = append(res, matrix[i][left])
|
||||||
}
|
}
|
||||||
@ -218,4 +217,3 @@ func spiralOrder(matrix [][]int) []int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -236,4 +236,3 @@ func subarraySum(nums []int, k int) int {
|
|||||||
return cnt
|
return cnt
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -361,7 +361,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -394,7 +393,7 @@ func generateMatrix(n int) [][]int {
|
|||||||
}
|
}
|
||||||
buttom--
|
buttom--
|
||||||
if num > size { break }
|
if num > size { break }
|
||||||
|
|
||||||
for i := buttom; i >= top; i-- {
|
for i := buttom; i >= top; i-- {
|
||||||
res[i][left] = num
|
res[i][left] = num
|
||||||
num++
|
num++
|
||||||
@ -405,4 +404,3 @@ func generateMatrix(n int) [][]int {
|
|||||||
return res
|
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/)
|
#### [66. 加一](https://leetcode-cn.com/problems/plus-one/)
|
||||||
|
|
||||||
@ -56,10 +56,10 @@ class Solution {
|
|||||||
if (digits[i] != 0) {
|
if (digits[i] != 0) {
|
||||||
return digits;
|
return digits;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//第三种情况,因为数组初始化每一位都为0,我们只需将首位设为1即可
|
//第三种情况,因为数组初始化每一位都为0,我们只需将首位设为1即可
|
||||||
int[] arr = new int[len+1];
|
int[] arr = new int[len+1];
|
||||||
arr[0] = 1;
|
arr[0] = 1;
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -138,4 +138,3 @@ func plusOne(digits []int) []int {
|
|||||||
return digits
|
return digits
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ class Solution {
|
|||||||
//这里和三向切分不完全一致
|
//这里和三向切分不完全一致
|
||||||
int i = left;
|
int i = left;
|
||||||
int right = len-1;
|
int right = len-1;
|
||||||
|
|
||||||
while (i <= right) {
|
while (i <= right) {
|
||||||
if (nums[i] == 2) {
|
if (nums[i] == 2) {
|
||||||
swap(nums,i,right--);
|
swap(nums,i,right--);
|
||||||
@ -57,7 +57,7 @@ class Solution {
|
|||||||
} else {
|
} else {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void swap (int[] nums, int i, int j) {
|
public void swap (int[] nums, int i, int j) {
|
||||||
int temp = nums[i];
|
int temp = nums[i];
|
||||||
@ -72,7 +72,7 @@ Python3 Code:
|
|||||||
```python
|
```python
|
||||||
from typing import List
|
from typing import List
|
||||||
class Solution:
|
class Solution:
|
||||||
def sortColors(self, nums: List[int]):
|
def sortColors(self, nums: List[int]):
|
||||||
leng = len(nums)
|
leng = len(nums)
|
||||||
left = 0
|
left = 0
|
||||||
# 这里和三向切分不完全一致
|
# 这里和三向切分不完全一致
|
||||||
@ -89,7 +89,7 @@ class Solution:
|
|||||||
else:
|
else:
|
||||||
i += 1
|
i += 1
|
||||||
return nums
|
return nums
|
||||||
|
|
||||||
def swap(self, nums: List[int], i: int, j: int):
|
def swap(self, nums: List[int], i: int, j: int):
|
||||||
temp = nums[i]
|
temp = nums[i]
|
||||||
nums[i] = nums[j]
|
nums[i] = nums[j]
|
||||||
@ -112,7 +112,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@ -173,8 +173,6 @@ func sortColors(nums []int) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
另外我们看这段代码,有什么问题呢?那就是我们即使完全符合时,仍会交换元素,这样会大大降低我们的效率。
|
另外我们看这段代码,有什么问题呢?那就是我们即使完全符合时,仍会交换元素,这样会大大降低我们的效率。
|
||||||
|
|
||||||
例如:[0,0,0,1,1,1,2,2,2]
|
例如:[0,0,0,1,1,1,2,2,2]
|
||||||
@ -203,7 +201,7 @@ class Solution {
|
|||||||
int len = nums.length;
|
int len = nums.length;
|
||||||
int right = len - 1;
|
int right = len - 1;
|
||||||
for (int i = 0; i <= right; ++i) {
|
for (int i = 0; i <= right; ++i) {
|
||||||
if (nums[i] == 0) {
|
if (nums[i] == 0) {
|
||||||
swap(nums,i,left);
|
swap(nums,i,left);
|
||||||
left++;
|
left++;
|
||||||
}
|
}
|
||||||
@ -216,7 +214,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void swap (int[] nums,int i, int j) {
|
public void swap (int[] nums,int i, int j) {
|
||||||
int temp = nums[i];
|
int temp = nums[i];
|
||||||
@ -246,7 +244,7 @@ class Solution:
|
|||||||
# 如果不等于 1 则需要继续判断,所以不移动 i 指针,i--
|
# 如果不等于 1 则需要继续判断,所以不移动 i 指针,i--
|
||||||
if nums[i] != 1:
|
if nums[i] != 1:
|
||||||
i -= 1
|
i -= 1
|
||||||
i += 1
|
i += 1
|
||||||
return nums
|
return nums
|
||||||
|
|
||||||
def swap(self, nums: List[int], i: int, j: int):
|
def swap(self, nums: List[int], i: int, j: int):
|
||||||
@ -264,7 +262,7 @@ public:
|
|||||||
int left = 0, len = nums.size();
|
int left = 0, len = nums.size();
|
||||||
int right = len - 1;
|
int right = len - 1;
|
||||||
for (int i = 0; i <= right; ++i) {
|
for (int i = 0; i <= right; ++i) {
|
||||||
if (nums[i] == 0) {
|
if (nums[i] == 0) {
|
||||||
swap(nums[i],nums[left++]);
|
swap(nums[i],nums[left++]);
|
||||||
}
|
}
|
||||||
if (nums[i] == 2) {
|
if (nums[i] == 2) {
|
||||||
@ -291,7 +289,7 @@ class Solution {
|
|||||||
//nums.swapAt(i, left) 直接调用系统方法
|
//nums.swapAt(i, left) 直接调用系统方法
|
||||||
self.swap(&nums, i, left) // 保持风格统一走自定义交换
|
self.swap(&nums, i, left) // 保持风格统一走自定义交换
|
||||||
left += 1
|
left += 1
|
||||||
}
|
}
|
||||||
if nums[i] == 2 {
|
if nums[i] == 2 {
|
||||||
//nums.swapAt(i, right) 直接调用系统方法
|
//nums.swapAt(i, right) 直接调用系统方法
|
||||||
self.swap(&nums, 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/)
|
#### [剑指 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, 1, 0, 2, 5, 3]
|
||||||
输出:2 或 3
|
输出:2 或 3
|
||||||
|
|
||||||
#### **HashSet**
|
#### **HashSet**
|
||||||
|
|
||||||
|
@ -120,7 +120,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -150,4 +149,3 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -163,7 +163,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -210,4 +209,3 @@ func threeSum(nums []int) [][]int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -96,7 +96,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -150,6 +149,3 @@ func fourSum(nums []int, target int) [][]int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -228,7 +228,6 @@ class Solution:
|
|||||||
return nums[len(nums) - 1]
|
return nums[len(nums) - 1]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -248,8 +247,6 @@ func singleNumber(nums []int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### HashSet
|
### HashSet
|
||||||
|
|
||||||
#### 解析
|
#### 解析
|
||||||
@ -616,7 +613,7 @@ func singleNumber(nums []int) int {
|
|||||||
res ^= x
|
res ^= x
|
||||||
}
|
}
|
||||||
return res
|
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>进入。
|
||||||
|
|
||||||
#### [137. 只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/)
|
#### [137. 只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/)
|
||||||
|
|
||||||
|
@ -225,7 +225,7 @@ class Solution {
|
|||||||
ListNode backhalf = reverse(midnode.next);
|
ListNode backhalf = reverse(midnode.next);
|
||||||
//遍历两部分链表,判断值是否相等
|
//遍历两部分链表,判断值是否相等
|
||||||
ListNode p1 = head;
|
ListNode p1 = head;
|
||||||
ListNode p2 = backhalf;
|
ListNode p2 = backhalf;
|
||||||
while (p2 != null) {
|
while (p2 != null) {
|
||||||
if (p1.val != p2.val) {
|
if (p1.val != p2.val) {
|
||||||
//若要还原,记得这里也要reverse
|
//若要还原,记得这里也要reverse
|
||||||
@ -234,11 +234,11 @@ class Solution {
|
|||||||
}
|
}
|
||||||
p1 = p1.next;
|
p1 = p1.next;
|
||||||
p2 = p2.next;
|
p2 = p2.next;
|
||||||
}
|
}
|
||||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||||
midnode.next = reverse(backhalf);
|
midnode.next = reverse(backhalf);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//找到中点
|
//找到中点
|
||||||
public ListNode searchmidnode (ListNode head) {
|
public ListNode searchmidnode (ListNode head) {
|
||||||
@ -247,7 +247,7 @@ class Solution {
|
|||||||
while (fast.next != null && fast.next.next != null) {
|
while (fast.next != null && fast.next.next != null) {
|
||||||
fast = fast.next.next;
|
fast = fast.next.next;
|
||||||
slow = slow.next;
|
slow = slow.next;
|
||||||
}
|
}
|
||||||
return slow;
|
return slow;
|
||||||
}
|
}
|
||||||
//翻转链表
|
//翻转链表
|
||||||
@ -282,7 +282,7 @@ public:
|
|||||||
ListNode * backhalf = reverse(midnode->next);
|
ListNode * backhalf = reverse(midnode->next);
|
||||||
//遍历两部分链表,判断值是否相等
|
//遍历两部分链表,判断值是否相等
|
||||||
ListNode * p1 = head;
|
ListNode * p1 = head;
|
||||||
ListNode * p2 = backhalf;
|
ListNode * p2 = backhalf;
|
||||||
while (p2 != nullptr) {
|
while (p2 != nullptr) {
|
||||||
if (p1->val != p2->val) {
|
if (p1->val != p2->val) {
|
||||||
//若要还原,记得这里也要reverse
|
//若要还原,记得这里也要reverse
|
||||||
@ -291,11 +291,11 @@ public:
|
|||||||
}
|
}
|
||||||
p1 = p1->next;
|
p1 = p1->next;
|
||||||
p2 = p2->next;
|
p2 = p2->next;
|
||||||
}
|
}
|
||||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||||
midnode->next = reverse(backhalf);
|
midnode->next = reverse(backhalf);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//找到中间的部分
|
//找到中间的部分
|
||||||
ListNode * searchmidnode (ListNode * head) {
|
ListNode * searchmidnode (ListNode * head) {
|
||||||
@ -304,7 +304,7 @@ public:
|
|||||||
while (fast->next != nullptr && fast->next->next != nullptr) {
|
while (fast->next != nullptr && fast->next->next != nullptr) {
|
||||||
fast = fast->next->next;
|
fast = fast->next->next;
|
||||||
slow = slow->next;
|
slow = slow->next;
|
||||||
}
|
}
|
||||||
return slow;
|
return slow;
|
||||||
}
|
}
|
||||||
//翻转链表
|
//翻转链表
|
||||||
@ -326,55 +326,55 @@ JS Code:
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var isPalindrome = function (head) {
|
var isPalindrome = function (head) {
|
||||||
if (head === null || head.next === null) {
|
if (head === null || head.next === null) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
|
||||||
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
|
||||||
let midnode = searchmidnode(head);
|
|
||||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
|
||||||
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
|
||||||
let backhalf = reverse(midnode.next);
|
|
||||||
//遍历两部分链表,判断值是否相等
|
|
||||||
let p1 = head;
|
|
||||||
let p2 = backhalf;
|
|
||||||
while (p2 != null) {
|
|
||||||
if (p1.val != p2.val) {
|
|
||||||
//若要还原,记得这里也要reverse
|
|
||||||
midnode.next = reverse(backhalf);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
p1 = p1.next;
|
|
||||||
p2 = p2.next;
|
|
||||||
}
|
|
||||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
|
||||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
|
||||||
midnode.next = reverse(backhalf);
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||||
|
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||||
|
let midnode = searchmidnode(head);
|
||||||
|
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||||
|
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||||
|
let backhalf = reverse(midnode.next);
|
||||||
|
//遍历两部分链表,判断值是否相等
|
||||||
|
let p1 = head;
|
||||||
|
let p2 = backhalf;
|
||||||
|
while (p2 != null) {
|
||||||
|
if (p1.val != p2.val) {
|
||||||
|
//若要还原,记得这里也要reverse
|
||||||
|
midnode.next = reverse(backhalf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p1 = p1.next;
|
||||||
|
p2 = p2.next;
|
||||||
|
}
|
||||||
|
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||||
|
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||||
|
midnode.next = reverse(backhalf);
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
//找到中点
|
//找到中点
|
||||||
var searchmidnode = function (head) {
|
var searchmidnode = function (head) {
|
||||||
let fast = head;
|
let fast = head;
|
||||||
let slow = head;
|
let slow = head;
|
||||||
while (fast.next != null && fast.next.next != null) {
|
while (fast.next != null && fast.next.next != null) {
|
||||||
fast = fast.next.next;
|
fast = fast.next.next;
|
||||||
slow = slow.next;
|
slow = slow.next;
|
||||||
}
|
}
|
||||||
return slow;
|
return slow;
|
||||||
};
|
};
|
||||||
|
|
||||||
//翻转链表
|
//翻转链表
|
||||||
var reverse = function (slow) {
|
var reverse = function (slow) {
|
||||||
let low = null;
|
let low = null;
|
||||||
let temp = null;
|
let temp = null;
|
||||||
while (slow != null) {
|
while (slow != null) {
|
||||||
temp = slow.next;
|
temp = slow.next;
|
||||||
slow.next = low;
|
slow.next = low;
|
||||||
low = slow;
|
low = slow;
|
||||||
slow = temp;
|
slow = temp;
|
||||||
}
|
}
|
||||||
return low;
|
return low;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -492,7 +492,7 @@ func isPalindrome(head *ListNode) bool {
|
|||||||
|
|
||||||
midNode := searchMidNode(head)
|
midNode := searchMidNode(head)
|
||||||
backHalf := reverse(midNode.Next)
|
backHalf := reverse(midNode.Next)
|
||||||
|
|
||||||
// 判断左右两边是否一样(回文)
|
// 判断左右两边是否一样(回文)
|
||||||
p1, p2 := head, backHalf
|
p1, p2 := head, backHalf
|
||||||
for p2 != nil {
|
for p2 != nil {
|
||||||
@ -530,4 +530,3 @@ func reverse(node *ListNode) *ListNode {
|
|||||||
return pre
|
return pre
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -125,7 +125,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -142,4 +141,3 @@ func hasCycle(head *ListNode) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -298,7 +298,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -322,4 +321,3 @@ func detectCycle(head *ListNode) *ListNode {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -258,7 +258,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -289,4 +288,3 @@ func insertionSortList(head *ListNode) *ListNode {
|
|||||||
return root.Next
|
return root.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -238,19 +238,19 @@ JS Code:
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var reverseList = function (head) {
|
var reverseList = function (head) {
|
||||||
//结束条件
|
//结束条件
|
||||||
if (!head || !head.next) {
|
if (!head || !head.next) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
//保存最后一个节点
|
//保存最后一个节点
|
||||||
let pro = reverseList(head.next);
|
let pro = reverseList(head.next);
|
||||||
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
||||||
//4.next = 5
|
//4.next = 5
|
||||||
//则 5.next = 4 则实现了反转
|
//则 5.next = 4 则实现了反转
|
||||||
head.next.next = head;
|
head.next.next = head;
|
||||||
//防止循环
|
//防止循环
|
||||||
head.next = null;
|
head.next = null;
|
||||||
return pro;
|
return pro;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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>进入。
|
||||||
|
|
||||||
### [328. 奇偶链表](https://leetcode-cn.com/problems/odd-even-linked-list/)
|
### [328. 奇偶链表](https://leetcode-cn.com/problems/odd-even-linked-list/)
|
||||||
|
|
||||||
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
示例 2:
|
示例 2:
|
||||||
|
|
||||||
> 输入: 2->1->3->5->6->4->7->NULL
|
> 输入: 2->1->3->5->6->4->7->NULL
|
||||||
> 输出: 2->3->6->7->1->5->4->NULL
|
> 输出: 2->3->6->7->1->5->4->NULL
|
||||||
|
|
||||||
#### 题目解析
|
#### 题目解析
|
||||||
@ -54,7 +54,7 @@ class Solution {
|
|||||||
odd = odd.next;
|
odd = odd.next;
|
||||||
even.next = odd.next;
|
even.next = odd.next;
|
||||||
even = even.next;
|
even = even.next;
|
||||||
}
|
}
|
||||||
//链接
|
//链接
|
||||||
odd.next = evenHead;
|
odd.next = evenHead;
|
||||||
return head;
|
return head;
|
||||||
@ -81,7 +81,7 @@ public:
|
|||||||
odd = odd->next;
|
odd = odd->next;
|
||||||
even->next = odd->next;
|
even->next = odd->next;
|
||||||
even = even->next;
|
even = even->next;
|
||||||
}
|
}
|
||||||
//链接
|
//链接
|
||||||
odd->next = evenHead;
|
odd->next = evenHead;
|
||||||
return head;
|
return head;
|
||||||
@ -98,15 +98,15 @@ var oddEvenList = function (head) {
|
|||||||
even = head.next,
|
even = head.next,
|
||||||
evenHead = even;
|
evenHead = even;
|
||||||
while (odd.next && even.next) {
|
while (odd.next && even.next) {
|
||||||
//将偶数位合在一起,奇数位合在一起
|
//将偶数位合在一起,奇数位合在一起
|
||||||
odd.next = even.next;
|
odd.next = even.next;
|
||||||
odd = odd.next;
|
odd = odd.next;
|
||||||
even.next = odd.next;
|
even.next = odd.next;
|
||||||
even = even.next;
|
even = even.next;
|
||||||
}
|
}
|
||||||
//链接
|
//链接
|
||||||
odd.next = evenHead;
|
odd.next = evenHead;
|
||||||
return head;
|
return head;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -195,7 +195,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -222,4 +221,3 @@ func deleteDuplicates(head *ListNode) *ListNode {
|
|||||||
return root.Next
|
return root.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -188,7 +188,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -214,4 +213,3 @@ func partition(head *ListNode, x int) *ListNode {
|
|||||||
return headSmall.Next
|
return headSmall.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -264,7 +264,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
GoCode:
|
GoCode:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -307,4 +306,3 @@ func reverse(head *ListNode) *ListNode {
|
|||||||
return pre
|
return pre
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -147,7 +147,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -173,4 +172,3 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
|
|||||||
return root.Next
|
return root.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -164,7 +164,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -182,4 +181,3 @@ func getKthFromEnd(head *ListNode, k int) *ListNode {
|
|||||||
return after
|
return after
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -136,7 +136,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -150,4 +149,3 @@ func middleNode(head *ListNode) *ListNode {
|
|||||||
return slow
|
return slow
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -268,7 +268,6 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -305,4 +304,3 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
|||||||
return root.Next
|
return root.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user