From 9573a885f082e6789108418f52c8cdd6002feb75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E9=9B=A8=E7=94=9F?= Date: Sat, 27 Apr 2019 11:31:16 +0800 Subject: [PATCH 1/6] week 2 --- Week_02/id_103/.idea/id_103.iml | 8 ++ Week_02/id_103/.idea/misc.xml | 6 + Week_02/id_103/.idea/modules.xml | 8 ++ Week_02/id_103/.idea/vcs.xml | 6 + Week_02/id_103/.idea/workspace.xml | 192 +++++++++++++++++++++++++++ Week_02/id_103/HashTable/P242.go | 48 +++++++ Week_02/id_103/HashTable/P671.go | 41 ++++++ Week_02/id_103/HashTable/P692.go | 40 ++++++ Week_02/id_103/HashTable/TreeNode.go | 7 + 9 files changed, 356 insertions(+) create mode 100644 Week_02/id_103/.idea/id_103.iml create mode 100644 Week_02/id_103/.idea/misc.xml create mode 100644 Week_02/id_103/.idea/modules.xml create mode 100644 Week_02/id_103/.idea/vcs.xml create mode 100644 Week_02/id_103/.idea/workspace.xml create mode 100644 Week_02/id_103/HashTable/P242.go create mode 100644 Week_02/id_103/HashTable/P671.go create mode 100644 Week_02/id_103/HashTable/P692.go create mode 100644 Week_02/id_103/HashTable/TreeNode.go diff --git a/Week_02/id_103/.idea/id_103.iml b/Week_02/id_103/.idea/id_103.iml new file mode 100644 index 00000000..c956989b --- /dev/null +++ b/Week_02/id_103/.idea/id_103.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Week_02/id_103/.idea/misc.xml b/Week_02/id_103/.idea/misc.xml new file mode 100644 index 00000000..28a804d8 --- /dev/null +++ b/Week_02/id_103/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Week_02/id_103/.idea/modules.xml b/Week_02/id_103/.idea/modules.xml new file mode 100644 index 00000000..9bdd8e3d --- /dev/null +++ b/Week_02/id_103/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Week_02/id_103/.idea/vcs.xml b/Week_02/id_103/.idea/vcs.xml new file mode 100644 index 00000000..b2bdec2d --- /dev/null +++ b/Week_02/id_103/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Week_02/id_103/.idea/workspace.xml b/Week_02/id_103/.idea/workspace.xml new file mode 100644 index 00000000..a9c90234 --- /dev/null +++ b/Week_02/id_103/.idea/workspace.xml @@ -0,0 +1,192 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -131,6 +149,7 @@ + @@ -157,36 +176,46 @@ - + - - + + - + - - + + - + - - + + - - + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_02/id_103/NOTE.md b/Week_02/id_103/NOTE.md index c684e62f..b6029998 100644 --- a/Week_02/id_103/NOTE.md +++ b/Week_02/id_103/NOTE.md @@ -1 +1,7 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 + +[Question 242 解题思路](https://www.jianshu.com/p/b6e91d46f7a4) + +[Question 671 解题思路](https://www.jianshu.com/p/63a5ff614e8a) + +[Question 692 解题思路](https://www.jianshu.com/p/55002c3771c5) \ No newline at end of file From da7bddc882c5bfef75f2681a2ba50c0deeb4140b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E9=9B=A8=E7=94=9F?= Date: Sat, 4 May 2019 13:38:15 +0800 Subject: [PATCH 3/6] Week 3 --- Week_02/id_103/.idea/workspace.xml | 14 +- Week_03/id_103/.idea/id_103.iml | 8 + Week_03/id_103/.idea/misc.xml | 6 + Week_03/id_103/.idea/modules.xml | 8 + Week_03/id_103/.idea/vcs.xml | 6 + Week_03/id_103/.idea/workspace.xml | 297 +++++++++++++++++++++++++++++ Week_03/id_103/P104.go | 20 ++ Week_03/id_103/P373.go | 62 ++++++ Week_03/id_103/P703.go | 55 ++++++ Week_03/id_103/TreeNode.go | 9 + Week_03/id_103/main.go | 5 + 11 files changed, 479 insertions(+), 11 deletions(-) create mode 100644 Week_03/id_103/.idea/id_103.iml create mode 100644 Week_03/id_103/.idea/misc.xml create mode 100644 Week_03/id_103/.idea/modules.xml create mode 100644 Week_03/id_103/.idea/vcs.xml create mode 100644 Week_03/id_103/.idea/workspace.xml create mode 100644 Week_03/id_103/P104.go create mode 100644 Week_03/id_103/P373.go create mode 100644 Week_03/id_103/P703.go create mode 100644 Week_03/id_103/TreeNode.go create mode 100644 Week_03/id_103/main.go diff --git a/Week_02/id_103/.idea/workspace.xml b/Week_02/id_103/.idea/workspace.xml index dadbd671..6bb83417 100644 --- a/Week_02/id_103/.idea/workspace.xml +++ b/Week_02/id_103/.idea/workspace.xml @@ -1,9 +1,7 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/P373.go + 57 + + + + file://$PROJECT_DIR$/P373.go + 44 + + + + file://$PROJECT_DIR$/P373.go + 28 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_03/id_103/P104.go b/Week_03/id_103/P104.go new file mode 100644 index 00000000..34f07d5c --- /dev/null +++ b/Week_03/id_103/P104.go @@ -0,0 +1,20 @@ +package main +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxDepth(root *TreeNode) int { + if root == nil { + return 0 + } + left := maxDepth(root.Left) + 1 + right := maxDepth(root.Right) + 1 + if left > right { + return left + } + return right +} diff --git a/Week_03/id_103/P373.go b/Week_03/id_103/P373.go new file mode 100644 index 00000000..8ce20834 --- /dev/null +++ b/Week_03/id_103/P373.go @@ -0,0 +1,62 @@ +package main + +import ( + "container/heap" + "fmt" +) + +type minHeap [][]int + +func (m minHeap) Len() int { + return len(m) +} + +func (m minHeap) Less(i, j int) bool { + v1 := m[i][0] + m[i][1] + v2 := m[j][0] + m[j][1] + return v1 > v2 +} + +func (m minHeap) Swap(i, j int) { + m[i],m[j] = m[j], m[i] +} + +func (m *minHeap) Push(x interface{}) { + *m = append(*m, x.([]int)) +} + +func (m *minHeap) Pop() interface{} { + old := *m + n := len(old) + x := old[n-1] + *m = old[0 : n-1] + return x +} + +func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { + v := &minHeap{} + heap.Init(v) + for _,v1 := range nums1 { + for _,v2 := range nums2 { + if v.Len() < k { + heap.Push(v,[]int{v1,v2}) + }else { + heap.Push(v,[]int{v1,v2}) + heap.Pop(v) + } + } + } + fmt.Println(v) + for v.Len() > k { + v.Pop() + } + fmt.Println(v) + return *v +} + +func main() { + num1 := []int{1,2,4,5,6} + num2 := []int{3,5,7,9} + kSmallestPairs(num1,num2,3) +} + diff --git a/Week_03/id_103/P703.go b/Week_03/id_103/P703.go new file mode 100644 index 00000000..09fc968c --- /dev/null +++ b/Week_03/id_103/P703.go @@ -0,0 +1,55 @@ +package main + +import "container/heap" + +// copied from golang doc +// mininum setup of integer min heap +type IntHeap []int + +func (h IntHeap) Len() int { return len(h) } +func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } +func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } + +func (h *IntHeap) Push(x interface{}) { + // Push and Pop use pointer receivers because they modify the slice's length, + // not just its contents. + *h = append(*h, x.(int)) +} + +func (h *IntHeap) Pop() interface{} { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] + return x +} + +// real thing starts here +type KthLargest struct { + Nums *IntHeap + K int +} + +func Constructor(k int, nums []int) KthLargest { + h := &IntHeap{} + heap.Init(h) + // push all elements to the heap + for i := 0; i < len(nums); i++ { + heap.Push(h, nums[i]) + } + // remove the smaller elements from the heap such that only the k largest elements are in the heap + for len(*h) > k { + heap.Pop(h) + } + return KthLargest{h, k} +} + +func (this *KthLargest) Add(val int) int { + if len(*this.Nums) < this.K { + heap.Push(this.Nums, val) + } else if val > (*this.Nums)[0] { + heap.Push(this.Nums, val) + heap.Pop(this.Nums) + } + return (*this.Nums)[0] +} \ No newline at end of file diff --git a/Week_03/id_103/TreeNode.go b/Week_03/id_103/TreeNode.go new file mode 100644 index 00000000..9904479b --- /dev/null +++ b/Week_03/id_103/TreeNode.go @@ -0,0 +1,9 @@ +package main + + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + diff --git a/Week_03/id_103/main.go b/Week_03/id_103/main.go new file mode 100644 index 00000000..79058077 --- /dev/null +++ b/Week_03/id_103/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + +} From 6c93a18be8f1b32b7e47c5ad089b97d8a7f0699f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E9=9B=A8=E7=94=9F?= Date: Sat, 4 May 2019 14:31:35 +0800 Subject: [PATCH 4/6] commit --- Week_03/id_103/.idea/workspace.xml | 161 ++++++++++++++++------------- Week_03/id_103/P104.go | 5 + Week_03/id_103/P429.go | 42 ++++++++ Week_03/id_103/TreeNode.go | 2 + 4 files changed, 140 insertions(+), 70 deletions(-) create mode 100644 Week_03/id_103/P429.go diff --git a/Week_03/id_103/.idea/workspace.xml b/Week_03/id_103/.idea/workspace.xml index 9abd0f8c..647d86a4 100644 --- a/Week_03/id_103/.idea/workspace.xml +++ b/Week_03/id_103/.idea/workspace.xml @@ -2,12 +2,10 @@ - - - - - - + + + + - + - + @@ -29,8 +27,8 @@ - - + + @@ -39,10 +37,19 @@ + + + + + + + + + - - + + @@ -50,8 +57,8 @@ - - + + @@ -59,7 +66,7 @@ - + @@ -83,6 +90,11 @@ + + + NodeVal + + - - - - - + @@ -157,7 +164,16 @@ - + + + + + + + + + + @@ -166,6 +182,15 @@ + + + + + + + + + @@ -176,34 +201,36 @@ + + - + - - + - - - - - + - + - + + + + + + @@ -215,81 +242,75 @@ file://$PROJECT_DIR$/P373.go 57 - - file://$PROJECT_DIR$/P373.go - 44 + file://$PROJECT_DIR$/P429.go + 40 - - - file://$PROJECT_DIR$/P373.go - 28 - - - + - - + + - + + - - + + - + - - + + - + - - + + - - + + - + - - + + - + - - + + - + - - + + diff --git a/Week_03/id_103/P104.go b/Week_03/id_103/P104.go index 34f07d5c..8fe30ebe 100644 --- a/Week_03/id_103/P104.go +++ b/Week_03/id_103/P104.go @@ -18,3 +18,8 @@ func maxDepth(root *TreeNode) int { } return right } + +func main() { + vr := TreeNode{} + maxDepth(&vr) +} \ No newline at end of file diff --git a/Week_03/id_103/P429.go b/Week_03/id_103/P429.go new file mode 100644 index 00000000..35e5d97b --- /dev/null +++ b/Week_03/id_103/P429.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +type NodeVal struct { + Val int + Children []*NodeVal +} + +func LevelOrder(root *NodeVal) [][]int { + res := make([][]int,0) + if root == nil { + return res + } + Add(root,0, &res) + return res +} + +func Add(node *NodeVal, index int, res *[][]int) { + if len(*res) <= index{ + *res = append(*res, []int{}) + } + childrens := *res + childrens[index] = append(childrens[index], node.Val) + if node.Children != nil{ + for i, _ := range node.Children { + Add(node.Children[i],index+1,res) + } + } +} + +func main() { + node1 := NodeVal{1,make([]*NodeVal,0)} + node2 := NodeVal{2,make([]*NodeVal,0)} + node3 := NodeVal{3,make([]*NodeVal,0)} + node4 := NodeVal{4,make([]*NodeVal,0)} + node5 := NodeVal{5,make([]*NodeVal,0)} + node6 := NodeVal{6,make([]*NodeVal,0)} + node1.Children = append(append(append(node1.Children, &node3), &node2), &node4) + node3.Children = append(append(node3.Children, &node5), &node6) + fmt.Println(LevelOrder(&node1)) +} \ No newline at end of file diff --git a/Week_03/id_103/TreeNode.go b/Week_03/id_103/TreeNode.go index 9904479b..e635bd4f 100644 --- a/Week_03/id_103/TreeNode.go +++ b/Week_03/id_103/TreeNode.go @@ -7,3 +7,5 @@ type TreeNode struct { Right *TreeNode } + + From ca59e339792dd9b6ba7be31a0847a4b0dafcce27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E9=9B=A8=E7=94=9F?= Date: Sat, 4 May 2019 14:50:12 +0800 Subject: [PATCH 5/6] note.md --- Week_03/id_103/.idea/workspace.xml | 92 ++++++++++++++++++------------ Week_03/id_103/NOTE.md | 10 +++- Week_03/id_103/P373.go | 2 +- 3 files changed, 66 insertions(+), 38 deletions(-) diff --git a/Week_03/id_103/.idea/workspace.xml b/Week_03/id_103/.idea/workspace.xml index 647d86a4..9666770e 100644 --- a/Week_03/id_103/.idea/workspace.xml +++ b/Week_03/id_103/.idea/workspace.xml @@ -2,10 +2,8 @@ - - - - + + @@ -212,7 +223,7 @@ - + @@ -225,7 +236,7 @@ - + @@ -247,7 +258,6 @@ file://$PROJECT_DIR$/P429.go 40 - @@ -262,6 +272,13 @@ + + + + + + + @@ -276,41 +293,44 @@ - + - - + + - + - - - - - + + - + - - + + - + - - + + + + + - - - - + + + + + + + diff --git a/Week_03/id_103/NOTE.md b/Week_03/id_103/NOTE.md index c684e62f..ed3a25b2 100644 --- a/Week_03/id_103/NOTE.md +++ b/Week_03/id_103/NOTE.md @@ -1 +1,9 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 + +[104 - 树最大深度](https://www.jianshu.com/p/e408e356f776) + +[373 - 查找最小的n个值](https://www.jianshu.com/p/6159b20721e1) + +[429 - 按照树的层级整理数据](https://www.jianshu.com/p/f2bc04222138) + +[703 - k个数,最大的值](https://www.jianshu.com/p/26b22e0bd8ac) \ No newline at end of file diff --git a/Week_03/id_103/P373.go b/Week_03/id_103/P373.go index 8ce20834..072294fc 100644 --- a/Week_03/id_103/P373.go +++ b/Week_03/id_103/P373.go @@ -38,7 +38,7 @@ func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { heap.Init(v) for _,v1 := range nums1 { for _,v2 := range nums2 { - if v.Len() < k { + if v.Len() < k {// 这里不能都添加完在移除,因为在pop哪里,移除方法并不是移除最大的,而是最后一个,如果保证堆内的数量,则可以保证,移除的一定是最大的。 heap.Push(v,[]int{v1,v2}) }else { heap.Push(v,[]int{v1,v2}) From 8110a56605160461727e65094790447827569862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E9=9B=A8=E7=94=9F?= Date: Sat, 4 May 2019 15:00:41 +0800 Subject: [PATCH 6/6] rename --- Week_03/id_103/.idea/workspace.xml | 17 ++++++++--------- Week_03/id_103/{P104.go => LeetCode_104_103.go} | 0 Week_03/id_103/{P373.go => LeetCode_373_103.go} | 0 Week_03/id_103/{P429.go => LeetCode_429_103.go} | 0 Week_03/id_103/{P703.go => LeetCode_703_103.go} | 0 5 files changed, 8 insertions(+), 9 deletions(-) rename Week_03/id_103/{P104.go => LeetCode_104_103.go} (100%) rename Week_03/id_103/{P373.go => LeetCode_373_103.go} (100%) rename Week_03/id_103/{P429.go => LeetCode_429_103.go} (100%) rename Week_03/id_103/{P703.go => LeetCode_703_103.go} (100%) diff --git a/Week_03/id_103/.idea/workspace.xml b/Week_03/id_103/.idea/workspace.xml index 9666770e..7e4c929b 100644 --- a/Week_03/id_103/.idea/workspace.xml +++ b/Week_03/id_103/.idea/workspace.xml @@ -2,8 +2,8 @@ - - + + - @@ -293,7 +292,7 @@ - + @@ -310,7 +309,7 @@ - + @@ -328,7 +327,7 @@ - + diff --git a/Week_03/id_103/P104.go b/Week_03/id_103/LeetCode_104_103.go similarity index 100% rename from Week_03/id_103/P104.go rename to Week_03/id_103/LeetCode_104_103.go diff --git a/Week_03/id_103/P373.go b/Week_03/id_103/LeetCode_373_103.go similarity index 100% rename from Week_03/id_103/P373.go rename to Week_03/id_103/LeetCode_373_103.go diff --git a/Week_03/id_103/P429.go b/Week_03/id_103/LeetCode_429_103.go similarity index 100% rename from Week_03/id_103/P429.go rename to Week_03/id_103/LeetCode_429_103.go diff --git a/Week_03/id_103/P703.go b/Week_03/id_103/LeetCode_703_103.go similarity index 100% rename from Week_03/id_103/P703.go rename to Week_03/id_103/LeetCode_703_103.go