Skip to content

Commit

Permalink
update leetcode book
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobinqt committed Jan 5, 2023
1 parent fc12bbe commit da88965
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 88 deletions.
5 changes: 4 additions & 1 deletion book.leetcode/docs/easy/206.md
Expand Up @@ -10,7 +10,10 @@ title: "206. 反转链表"

# 206. 反转链表

题目地址:[https://leetcode.cn/problems/reverse-linked-list/](https://leetcode.cn/problems/reverse-linked-list/)
## 题目地址

+ [力扣](https://leetcode.cn/problems/reverse-linked-list/)
+ [LeetCode](https://leetcode.com/problems/reverse-linked-list/)

类似/相同题目:[剑指 Offer 24. 反转链表](https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/)

Expand Down
2 changes: 1 addition & 1 deletion book.leetcode/docs/easy/_index.md
Expand Up @@ -3,7 +3,7 @@ weight: 1

bookFlatSection: true

bookCollapseSection: true
bookCollapseSection: false

bookToc: false

Expand Down
12 changes: 11 additions & 1 deletion book.leetcode/docs/hard/76.md
Expand Up @@ -10,7 +10,17 @@ title: "😪76. 最小覆盖子串"

# 76. 最小覆盖子串

题目地址:[https://leetcode.cn/problems/minimum-window-substring/](https://leetcode.cn/problems/minimum-window-substring/)
## 题目地址

+ [力扣](https://leetcode.cn/problems/minimum-window-substring/)
+ [LeetCode](https://leetcode.com/problems/minimum-window-substring/)

## 解题思路

TODO

## 具体思路




Expand Down
2 changes: 1 addition & 1 deletion book.leetcode/docs/hard/_index.md
Expand Up @@ -3,7 +3,7 @@ weight: 3

bookFlatSection: true

bookCollapseSection: true
bookCollapseSection: false

bookToc: false

Expand Down
2 changes: 1 addition & 1 deletion book.leetcode/docs/hot100/_index.md
Expand Up @@ -3,7 +3,7 @@ weight: 4

bookFlatSection: true

bookCollapseSection: true
bookCollapseSection: false

bookToc: false

Expand Down
5 changes: 4 additions & 1 deletion book.leetcode/docs/medium/209.md
Expand Up @@ -10,7 +10,10 @@ title: "209. 长度最小的子数组"

# 209. 长度最小的子数组

题目地址:[https://leetcode.cn/problems/minimum-size-subarray-sum/description/](https://leetcode.cn/problems/minimum-size-subarray-sum/description/)
## 题目地址

+ [力扣](https://leetcode.cn/problems/minimum-size-subarray-sum/description/)
+ [LeetCode](https://leetcode.com/problems/minimum-size-subarray-sum/description/)

## 解题思路

Expand Down
62 changes: 34 additions & 28 deletions book.leetcode/docs/medium/24.md
Expand Up @@ -10,7 +10,10 @@ title: "24. 两两交换链表中的节点"

# 24. 两两交换链表中的节点

题目地址:[https://leetcode.cn/problems/swap-nodes-in-pairs/](https://leetcode.cn/problems/swap-nodes-in-pairs/)
## 题目地址

+ [力扣](https://leetcode.cn/problems/swap-nodes-in-pairs/)
+ [LeetCode](https://leetcode.com/problems/swap-nodes-in-pairs/)

## 解题思路

Expand All @@ -24,7 +27,7 @@ title: "24. 两两交换链表中的节点"

`head`表示原始链表的头节点,新的链表的第二个节点,用`newHead`表示新的链表的头节点,原始链表的第二个节点,则原始链表中的其余节点的头节点是`newHead.next`。令`head.next = swapPairs(newHead.next)`,表示将其余节点进行两两交换,交换后的新的头节点为`head`的下一个节点。然后令`newHead.next = head`,即完成了所有节点的交换。最后返回新的链表的头节点`newHead`

:warning:好吧,再理解一下,关于递归,我只关注我这一层要干什么,返回什么,至于我的下一层(规模减 1),我不管,我就是一个甩手掌柜:see_no_evil:
⚠️好吧,再理解一下,关于递归,我只关注我这一层要干什么,返回什么,至于我的下一层(规模减 1),我不管,我就是一个甩手掌柜🙈

我其实只需要关心第一层,也就是`节点1``节点2`的交换,把`节点2``next`指向`节点1``节点2``next`给下一层也就是递归函数。而我最后返回的应该是头结点,其实也就是原始节点的`节点2`

Expand All @@ -36,7 +39,7 @@ TODO

{{< tabs "uniqueid" >}}

{{< tab "Go递归" >}}
{{< tab "Go迭代" >}}

```go
package main
Expand All @@ -51,14 +54,30 @@ type ListNode struct {
}

func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
if head == nil {
return head
}

newHead := head.Next
head.Next = swapPairs(head.Next.Next)
newHead.Next = head
return newHead
dummy := &ListNode{
Val: 0,
Next: head,
}

curr := dummy
// 如果是链表长度是奇数那就没有必要交换,只有偶数才需要交换,所以这里的判断条件是 &&
// 这里不能用一个临时参数,因为链表指针后的值会变化
for curr.Next != nil && curr.Next.Next != nil {
tmp := curr.Next
tmp1 := curr.Next.Next.Next

curr.Next = curr.Next.Next
curr.Next.Next = tmp
curr.Next.Next.Next = tmp1

curr = curr.Next.Next
}

return dummy.Next
}

func main() {
Expand All @@ -82,7 +101,7 @@ func main() {

{{< /tab >}}

{{< tab "Go迭代" >}}
{{< tab "Go递归" >}}

```go
package main
Expand All @@ -97,28 +116,14 @@ type ListNode struct {
}

func swapPairs(head *ListNode) *ListNode {
if head == nil {
if head == nil || head.Next == nil {
return head
}

dummy := &ListNode{
Val: 0,
Next: head,
}

curr := dummy
for curr.Next != nil && curr.Next.Next != nil {
tmp := curr.Next
tmp1 := curr.Next.Next.Next

curr.Next = curr.Next.Next
curr.Next.Next = tmp
curr.Next.Next.Next = tmp1

curr = curr.Next.Next
}

return dummy.Next
newHead := head.Next
head.Next = swapPairs(head.Next.Next)
newHead.Next = head
return newHead
}

func main() {
Expand All @@ -142,5 +147,6 @@ func main() {

{{< /tab >}}


{{< /tabs >}}

12 changes: 12 additions & 0 deletions book.leetcode/docs/medium/707.md
Expand Up @@ -16,3 +16,15 @@ title: "😪707. 设计链表"

## 具体实现

{{< tabs "uniqueid" >}}

{{< tab "Go" >}}

```go

```

{{< /tab >}}

{{< /tabs >}}

2 changes: 1 addition & 1 deletion book.leetcode/docs/medium/_index.md
Expand Up @@ -3,7 +3,7 @@ weight: 2

bookFlatSection: true

bookCollapseSection: true
bookCollapseSection: false

bookToc: false

Expand Down
52 changes: 0 additions & 52 deletions book.leetcode/docs/medium/hidden.md

This file was deleted.

2 changes: 1 addition & 1 deletion book.leetcode/docs/other/_index.md
Expand Up @@ -3,7 +3,7 @@ weight: 5

bookFlatSection: true

bookCollapseSection: true
bookCollapseSection: false

bookToc: false

Expand Down

0 comments on commit da88965

Please sign in to comment.