mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-26 20:36:18 +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/)
|
||||
|
||||
@ -52,10 +52,10 @@ class Solution {
|
||||
//查询成功
|
||||
if (target == nums[mid]) {
|
||||
return mid;
|
||||
//右区间
|
||||
//右区间
|
||||
} else if (nums[mid] < target) {
|
||||
left = mid + 1;
|
||||
//左区间
|
||||
left = mid + 1;
|
||||
//左区间
|
||||
} else if (nums[mid] > target) {
|
||||
right = mid - 1;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class Solution {
|
||||
TreeNode cur = new TreeNode(-1);
|
||||
cur = root;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
//找到当前应该遍历的那个节点
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
@ -47,7 +47,7 @@ class Solution {
|
||||
cur = temp.right;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -104,4 +104,4 @@ func inorderTraversal(root *TreeNode) []int {
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
###
|
||||
|
@ -49,10 +49,10 @@ class Solution {
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
if (root == null) return list;
|
||||
if (root == null) return list;
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode temp = stack.pop();
|
||||
TreeNode temp = stack.pop();
|
||||
if (temp.right != null) {
|
||||
stack.push(temp.right);
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
我们知道后序遍历的顺序是,` 对于树中的某节点, 先遍历该节点的左子树, 再遍历其右子树, 最后遍历该节点`。
|
||||
|
||||
那么我们如何利用栈来解决呢?
|
||||
那么我们如何利用栈来解决呢?
|
||||
|
||||
我们直接来看动画,看动画之前,但是我们`需要带着问题看动画`,问题搞懂之后也就搞定了后序遍历。
|
||||
|
||||
|
@ -80,3 +80,4 @@ func dailyTemperatures(temperatures []int) []int {
|
||||
}
|
||||
return arr
|
||||
}
|
||||
```
|
||||
|
@ -111,7 +111,7 @@ func (mq *MaxQueue) Is_empty() bool {
|
||||
return mq.size == 0
|
||||
}
|
||||
|
||||
// Max_value 取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
|
||||
// Max_value 取最大值值,返回我们双端队列的对头即可,因为我们双端队列是单调递减的嘛
|
||||
func (mq *MaxQueue) Max_value() int {
|
||||
if mq.Is_empty() { return -1 }
|
||||
return mq.deq[0]
|
||||
|
@ -116,4 +116,5 @@ func (m *MinStack) GetMin() int {
|
||||
return m.minStk[len(m.minStk) - 1]
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
|
@ -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 @@
|
||||
|
||||
下面我们通过一个视频模拟代码执行步骤大家一下就能搞懂了。
|
||||
|
||||
![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
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -163,7 +163,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -210,4 +209,3 @@ func threeSum(nums []int) [][]int {
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -96,7 +96,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -150,6 +149,3 @@ func fourSum(nums []int, target int) [][]int {
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -228,7 +228,6 @@ class Solution:
|
||||
return nums[len(nums) - 1]
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -248,8 +247,6 @@ func singleNumber(nums []int) int {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### HashSet
|
||||
|
||||
#### 解析
|
||||
@ -616,7 +613,7 @@ func singleNumber(nums []int) int {
|
||||
res ^= x
|
||||
}
|
||||
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/)
|
||||
|
||||
|
@ -225,7 +225,7 @@ class Solution {
|
||||
ListNode backhalf = reverse(midnode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode p1 = head;
|
||||
ListNode p2 = backhalf;
|
||||
ListNode p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
@ -234,11 +234,11 @@ class Solution {
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode.next = reverse(backhalf);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
//找到中点
|
||||
public ListNode searchmidnode (ListNode head) {
|
||||
@ -247,7 +247,7 @@ class Solution {
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
//翻转链表
|
||||
@ -282,7 +282,7 @@ public:
|
||||
ListNode * backhalf = reverse(midnode->next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode * p1 = head;
|
||||
ListNode * p2 = backhalf;
|
||||
ListNode * p2 = backhalf;
|
||||
while (p2 != nullptr) {
|
||||
if (p1->val != p2->val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
@ -291,11 +291,11 @@ public:
|
||||
}
|
||||
p1 = p1->next;
|
||||
p2 = p2->next;
|
||||
}
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode->next = reverse(backhalf);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
//找到中间的部分
|
||||
ListNode * searchmidnode (ListNode * head) {
|
||||
@ -304,7 +304,7 @@ public:
|
||||
while (fast->next != nullptr && fast->next->next != nullptr) {
|
||||
fast = fast->next->next;
|
||||
slow = slow->next;
|
||||
}
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
//翻转链表
|
||||
@ -326,55 +326,55 @@ JS Code:
|
||||
|
||||
```javascript
|
||||
var isPalindrome = function (head) {
|
||||
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);
|
||||
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;
|
||||
};
|
||||
|
||||
//找到中点
|
||||
var searchmidnode = function (head) {
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return slow;
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return slow;
|
||||
};
|
||||
|
||||
//翻转链表
|
||||
var reverse = function (slow) {
|
||||
let low = null;
|
||||
let temp = null;
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
low = slow;
|
||||
slow = temp;
|
||||
}
|
||||
return low;
|
||||
let temp = null;
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
low = slow;
|
||||
slow = temp;
|
||||
}
|
||||
return low;
|
||||
};
|
||||
```
|
||||
|
||||
@ -492,7 +492,7 @@ func isPalindrome(head *ListNode) bool {
|
||||
|
||||
midNode := searchMidNode(head)
|
||||
backHalf := reverse(midNode.Next)
|
||||
|
||||
|
||||
// 判断左右两边是否一样(回文)
|
||||
p1, p2 := head, backHalf
|
||||
for p2 != nil {
|
||||
@ -530,4 +530,3 @@ func reverse(node *ListNode) *ListNode {
|
||||
return pre
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -125,7 +125,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -142,4 +141,3 @@ func hasCycle(head *ListNode) bool {
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -298,7 +298,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -322,4 +321,3 @@ func detectCycle(head *ListNode) *ListNode {
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -258,7 +258,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -289,4 +288,3 @@ func insertionSortList(head *ListNode) *ListNode {
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -238,19 +238,19 @@ JS Code:
|
||||
|
||||
```javascript
|
||||
var reverseList = function (head) {
|
||||
//结束条件
|
||||
if (!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
//保存最后一个节点
|
||||
let pro = reverseList(head.next);
|
||||
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
||||
//4.next = 5
|
||||
//则 5.next = 4 则实现了反转
|
||||
head.next.next = head;
|
||||
//防止循环
|
||||
head.next = null;
|
||||
return pro;
|
||||
//结束条件
|
||||
if (!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
//保存最后一个节点
|
||||
let pro = reverseList(head.next);
|
||||
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
||||
//4.next = 5
|
||||
//则 5.next = 4 则实现了反转
|
||||
head.next.next = head;
|
||||
//防止循环
|
||||
head.next = null;
|
||||
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/)
|
||||
|
||||
@ -21,7 +21,7 @@
|
||||
|
||||
示例 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
|
||||
|
||||
#### 题目解析
|
||||
@ -54,7 +54,7 @@ class Solution {
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
@ -81,7 +81,7 @@ public:
|
||||
odd = odd->next;
|
||||
even->next = odd->next;
|
||||
even = even->next;
|
||||
}
|
||||
}
|
||||
//链接
|
||||
odd->next = evenHead;
|
||||
return head;
|
||||
@ -98,15 +98,15 @@ var oddEvenList = function (head) {
|
||||
even = head.next,
|
||||
evenHead = even;
|
||||
while (odd.next && even.next) {
|
||||
//将偶数位合在一起,奇数位合在一起
|
||||
odd.next = even.next;
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
//将偶数位合在一起,奇数位合在一起
|
||||
odd.next = even.next;
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -195,7 +195,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -222,4 +221,3 @@ func deleteDuplicates(head *ListNode) *ListNode {
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -188,7 +188,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -214,4 +213,3 @@ func partition(head *ListNode, x int) *ListNode {
|
||||
return headSmall.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -264,7 +264,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
GoCode:
|
||||
|
||||
```go
|
||||
@ -307,4 +306,3 @@ func reverse(head *ListNode) *ListNode {
|
||||
return pre
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -147,7 +147,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -173,4 +172,3 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -164,7 +164,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -182,4 +181,3 @@ func getKthFromEnd(head *ListNode, k int) *ListNode {
|
||||
return after
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -136,7 +136,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -150,4 +149,3 @@ func middleNode(head *ListNode) *ListNode {
|
||||
return slow
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -268,7 +268,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
@ -305,4 +304,3 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user