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..6bb83417 --- /dev/null +++ b/Week_02/id_103/.idea/workspace.xml @@ -0,0 +1,213 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/P373.go + 57 + + + file://$PROJECT_DIR$/P429.go + 40 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week_03/id_103/LeetCode_104_103.go b/Week_03/id_103/LeetCode_104_103.go new file mode 100644 index 00000000..8fe30ebe --- /dev/null +++ b/Week_03/id_103/LeetCode_104_103.go @@ -0,0 +1,25 @@ +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 +} + +func main() { + vr := TreeNode{} + maxDepth(&vr) +} \ No newline at end of file diff --git a/Week_03/id_103/LeetCode_373_103.go b/Week_03/id_103/LeetCode_373_103.go new file mode 100644 index 00000000..072294fc --- /dev/null +++ b/Week_03/id_103/LeetCode_373_103.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 {// 这里不能都添加完在移除,因为在pop哪里,移除方法并不是移除最大的,而是最后一个,如果保证堆内的数量,则可以保证,移除的一定是最大的。 + 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/LeetCode_429_103.go b/Week_03/id_103/LeetCode_429_103.go new file mode 100644 index 00000000..35e5d97b --- /dev/null +++ b/Week_03/id_103/LeetCode_429_103.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/LeetCode_703_103.go b/Week_03/id_103/LeetCode_703_103.go new file mode 100644 index 00000000..09fc968c --- /dev/null +++ b/Week_03/id_103/LeetCode_703_103.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/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/TreeNode.go b/Week_03/id_103/TreeNode.go new file mode 100644 index 00000000..e635bd4f --- /dev/null +++ b/Week_03/id_103/TreeNode.go @@ -0,0 +1,11 @@ +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() { + +}