Skip to content

Commit 2eaf09d

Browse files
committed
Leetcode
1 parent ed7ebab commit 2eaf09d

File tree

9 files changed

+734
-7
lines changed

9 files changed

+734
-7
lines changed

Leetcode/0088.Merge-Sorted-Array/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,38 @@ nums2.length == n
3131

3232
## 來源
3333
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0088.Merge-Sorted-Array/
34-
* https://leetcode-cn.com/problems/merge-sorted-array/
34+
* https://leetcode-cn.com/problems/merge-sorted-array/
35+
36+
37+
## 解答
38+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0088.Merge-Sorted-Array/Merge-Sorted-Array.go
39+
40+
```go
41+
package mergesortedarray
42+
43+
func Merge(nums1 []int, m int, nums2 []int, n int) {
44+
if m == 0 {
45+
copy(nums1, nums2)
46+
return
47+
}
48+
49+
num1Idx, num2Idx, tail := m-1, n-1, m+n-1
50+
// 從每一個array的後面開始比較, 並放入最後面的位子. 這樣只要跑一次
51+
for ; num1Idx >= 0 && num2Idx >= 0; tail-- {
52+
if nums1[num1Idx] > nums2[num2Idx] {
53+
nums1[tail] = nums1[num1Idx]
54+
num1Idx--
55+
} else {
56+
nums1[tail] = nums2[num2Idx]
57+
num2Idx--
58+
}
59+
}
60+
61+
// 將尚未跑完的補齊
62+
for ; num2Idx > 0; tail-- {
63+
nums1[tail] = nums2[num2Idx]
64+
num2Idx--
65+
}
66+
}
67+
68+
```

Leetcode/0094.Binary-Tree-Inorder-Traversal/README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,56 @@ Follow up: Recursive solution is trivial, could you do it iteratively?
3030

3131
## 來源
3232
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0094.Binary-Tree-Inorder-Traversal/
33-
* https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
33+
* https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
34+
35+
36+
## 解答
37+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0094.Binary-Tree-Inorder-Traversal/Binary-Tree-Inorder-Traversal.go
38+
39+
```go
40+
package binarytreeinordertraversal
41+
42+
import (
43+
"LeetcodeGolang/Utility/structures"
44+
)
45+
46+
type TreeNode = structures.TreeNode
47+
48+
func InorderTraversalStack(root *TreeNode) []int {
49+
var result []int
50+
inorderStack(root, &result)
51+
return result
52+
}
53+
54+
func InorderTraversalRecursion(root *TreeNode) []int {
55+
var result []int
56+
inorderRecursion(root, &result)
57+
return result
58+
}
59+
60+
func inorderRecursion(root *TreeNode, r *[]int) {
61+
if root != nil {
62+
inorderRecursion(root.Left, r)
63+
*r = append(*r, root.Val)
64+
inorderRecursion(root.Right, r)
65+
}
66+
}
67+
68+
func inorderStack(root *TreeNode, r *[]int) {
69+
s := structures.NewArrayStack()
70+
p := root
71+
72+
for !s.IsEmpty() || p != nil {
73+
if p != nil {
74+
// 把中間放入stack, 往左節點繼續往下找
75+
s.Push(p)
76+
p = p.Left
77+
} else {
78+
// 找不到時,印出
79+
tmp := s.Pop().(*TreeNode)
80+
*r = append(*r, tmp.Val)
81+
p = tmp.Right
82+
}
83+
}
84+
}
85+
```

Leetcode/0141.Linked-List-Cycle/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,29 @@ Can you solve it without using extra space?
1717

1818
## 來源
1919
* https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0141.Linked-List-Cycle/
20-
* https://leetcode-cn.com/problems/linked-list-cycle/
20+
* https://leetcode-cn.com/problems/linked-list-cycle/
21+
22+
## 解答
23+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0141.Linked-List-Cycle/Linked-List-Cycle.go
24+
25+
```go
26+
package linkedlistcycle
27+
28+
type ListNode struct {
29+
Val int
30+
Next *ListNode
31+
}
32+
33+
func HasCycle(head *ListNode) bool {
34+
fast := head
35+
slow := head
36+
for slow != nil && fast != nil && fast.Next != nil {
37+
fast = fast.Next.Next
38+
slow = slow.Next
39+
if slow == fast {
40+
return true
41+
}
42+
}
43+
return false
44+
}
45+
```

Leetcode/0203.Remove-Linked-List-Elements/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,84 @@ Output: 1->2->3->4->5
2323
## 來源
2424
* https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0203.Remove-Linked-List-Elements/
2525
* https://leetcode-cn.com/problems/remove-linked-list-elements/
26-
* https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA
26+
* https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA
27+
28+
## 解答
29+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0203.Remove-Linked-List-Elements/Remove-Linked-List-Elements.go
30+
31+
```go
32+
package removelinkedlistelements
33+
34+
import (
35+
"LeetcodeGolang/structures"
36+
)
37+
38+
/**
39+
* Definition for singly-linked list.
40+
* type ListNode struct {
41+
* Val int
42+
* Next *ListNode
43+
* }
44+
*/
45+
46+
type ListNode = structures.ListNode
47+
48+
// 方法一: 另外考慮刪除head節點
49+
func RemoveElements(head *ListNode, val int) *ListNode {
50+
// 刪除值相同的head
51+
for head != nil && head.Val == val {
52+
head = head.Next
53+
}
54+
if head == nil {
55+
return head
56+
}
57+
tmpHead := head
58+
for tmpHead.Next != nil {
59+
if tmpHead.Next.Val == val {
60+
tmpHead.Next = tmpHead.Next.Next
61+
} else {
62+
tmpHead = tmpHead.Next
63+
}
64+
}
65+
return head
66+
}
67+
68+
/*
69+
方法二 添加虛擬節點, 效能較好
70+
可以設置一個虛擬頭結點」,這樣原鍊錶的所有節點就都可以按照統一的方式進行移除了
71+
return newHead.Next
72+
*/
73+
func RemoveElementsVirtualNode(head *ListNode, val int) *ListNode {
74+
if head == nil {
75+
return head
76+
}
77+
// 建立一個虛擬 Head 節點
78+
newHead := &ListNode{Val: 0, Next: head}
79+
preHead := newHead
80+
curHead := head
81+
for curHead != nil {
82+
if curHead.Val == val {
83+
preHead.Next = curHead.Next
84+
} else {
85+
preHead = curHead
86+
}
87+
curHead = curHead.Next
88+
}
89+
return newHead.Next
90+
}
91+
92+
/*
93+
方法二 遞迴
94+
*/
95+
func RemoveElementsRecurse(head *ListNode, val int) *ListNode {
96+
if head == nil {
97+
return head
98+
}
99+
head.Next = RemoveElementsRecurse(head.Next, val)
100+
if head.Val == val {
101+
return head.Next
102+
} else {
103+
return head
104+
}
105+
}
106+
```

Leetcode/0209.Minimum-Size-Subarray-Sum/README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,129 @@ If you have figured out the O(n) solution, try coding another solution of which
4141
## 來源
4242
* https://books.halfrost.com/leetcode/ChapterFour/0200~0299/0209.Minimum-Size-Subarray-Sum/
4343
* https://leetcode-cn.com/problems/minimum-size-subarray-sum/
44-
* https://mp.weixin.qq.com/s/UrZynlqi4QpyLlLhBPglyg
44+
* https://mp.weixin.qq.com/s/UrZynlqi4QpyLlLhBPglyg
45+
46+
## 解答
47+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0209.Minimum-Size-Subarray-Sum/Minimum-Size-Subarray-Sum.go
48+
49+
```go
50+
package minimumsizesubarraysum
51+
52+
import (
53+
"math"
54+
"sort"
55+
)
56+
57+
// MinSubArrayLenBurst : 暴力解 時間複雜 O(n^2), 空間複雜 O(1)
58+
func MinSubArrayLenBurst(target int, nums []int) int {
59+
lens := len(nums)
60+
if lens <= 0 {
61+
return 0
62+
}
63+
64+
minSize := math.MaxInt32
65+
for i := 0; i < lens; i++ {
66+
sum := 0
67+
for j := i; j < lens; j++ {
68+
sum += nums[j]
69+
if sum >= target {
70+
minSize = min(minSize, j-i+1)
71+
}
72+
}
73+
}
74+
if minSize == math.MaxInt32 {
75+
minSize = 0
76+
}
77+
return minSize
78+
}
79+
80+
// MinSubArrayLenSlidingWindow : 滑動視窗 時間複雜 O(n), 空間複雜 O(1)
81+
// 滑動窗口的精妙之處在於根據當前子序列和大小的情況,不斷調節子序列的起始位置。從而將O(n^2)的暴力解法降為O(n)
82+
func MinSubArrayLenSlidingWindow(target int, nums []int) int {
83+
lens := len(nums)
84+
if lens <= 0 {
85+
return 0
86+
}
87+
minSize := math.MaxInt32
88+
89+
start, end, sum := 0, 0, 0
90+
for end < lens {
91+
sum += nums[end]
92+
for sum >= target {
93+
minSize = min(minSize, end-start+1)
94+
sum -= nums[start]
95+
start++
96+
}
97+
end++
98+
99+
}
100+
101+
if minSize == math.MaxInt32 {
102+
minSize = 0
103+
}
104+
return minSize
105+
}
106+
107+
/*
108+
MinSubArrayLenBinarysearch : 前缀和 + 二分查找 O(nlog(n))
109+
為了使用二分查找,需要額外創建一個數組 sums 用於存儲數組nums 的前綴和,其中 sums[i] 表示從 nums[0] 到 nums[i−1] 的元素和。
110+
得到前綴和之後,對於每個開始下標i,可通過二分查找得到大於或等於 i 的最小下標 bound,
111+
使得 sums[bound]-sums[i-1] >= s,
112+
並更新子數組的最小長度(此時子數組的長度是 bound -(i-1) )。
113+
C++ 的 lower_bound,Java 的 Arrays.binarySearch
114+
115+
因為這道題保證了數組中每個元素都為正,所以前綴和一定是遞增的,這一點保證了二分的正確性。如果題目沒有說明數組中每個元素都為正,這裡就不能使用二分來查找這個位置了。
116+
*/
117+
func MinSubArrayLenBinarysearch(target int, nums []int) int {
118+
lens := len(nums)
119+
if lens <= 0 {
120+
return 0
121+
}
122+
minSize := math.MaxInt32
123+
124+
// sums[i] 表示從 nums[0] 到 nums[i−1]
125+
sums := make([]int, lens+1)
126+
// 為了方便計算 size = lens + 1
127+
// sums[0] = 0, 前0個的元素和
128+
// sums[1] = nums[0] , 前1個元素和為 nums[0]
129+
for i := 1; i <= lens; i++ {
130+
sums[i] = sums[i-1] + nums[i-1]
131+
}
132+
// fmt.Println("sums", sums)
133+
for i := 1; i <= lens; i++ {
134+
/*
135+
target 7
136+
input 2 3 1 2 4 3
137+
-----------------------------------
138+
i 0 1 2 3 4 5 6
139+
-----------------------------------
140+
sums 0 2 5 6 8 12 15 // input 的前 n 個元素和
141+
tmpTarge 7 9 12 13 15 19 // tmpTarge = target + sums[i-1]
142+
bound 4 5 5 6 6 7 // bound = sort.SearchInts(sums, tmpTarge)
143+
bound-(i-1) 4 4 3 3 2 2
144+
minSize 4 4 3 3 2 2
145+
*/
146+
tmpTarge := target + sums[i-1]
147+
bound := sort.SearchInts(sums, tmpTarge)
148+
// if bound < 0 {
149+
// bound = -bound - 1
150+
// }
151+
if bound <= lens {
152+
minSize = min(minSize, bound-(i-1))
153+
}
154+
// fmt.Println("i:", i, " tmpTarge", tmpTarge, " bound:", bound, " bound-(i-1)", bound-(i-1), " minSize:", minSize)
155+
}
156+
157+
if minSize == math.MaxInt32 {
158+
minSize = 0
159+
}
160+
return minSize
161+
}
162+
163+
func min(x, y int) int {
164+
if x < y {
165+
return x
166+
}
167+
return y
168+
}
169+
```

Leetcode/0344.Reverse-String/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ Output: ["h","a","n","n","a","H"]
3333
## 來源
3434
* https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0344.Reverse-String/
3535
* https://leetcode-cn.com/problems/reverse-string/
36+
37+
## 解答
38+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0344.Reverse-Stringm/Reverse-String.go
39+
40+
```go
41+
package reversestring
42+
43+
func ReverseString(s []byte) {
44+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
45+
s[i], s[j] = s[j], s[i]
46+
}
47+
}
48+
49+
```

0 commit comments

Comments
 (0)