From 4b09b1f5657507adae5cb65074f8e5a9e7aead82 Mon Sep 17 00:00:00 2001 From: hzliangbin Date: Fri, 3 Jul 2020 20:40:46 +0800 Subject: [PATCH] feat:add golang solution for lcof problem 06 07 09 --- .../README.md" | 21 ++++++++-- .../Solution.go" | 16 +++++++ .../README.md" | 33 +++++++++++++++ .../Solution.go" | 25 +++++++++++ .../README.md" | 42 +++++++++++++++++++ .../Solution.go" | 34 +++++++++++++++ 6 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.go" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.go" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.go" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" index 3e4942c7c78c8..59e5422d90108 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" @@ -60,9 +60,24 @@ class Solution { } ``` -### JavaScript -```js - +### Go +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ + //insert to the front +func reversePrint(head *ListNode) []int { + res := []int{} + for head != nil { + res = append([]int{head.Val}, res...) + head = head.Next + } + return res +} ``` ### ... diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.go" new file mode 100644 index 0000000000000..01d4ae7d56ca4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.go" @@ -0,0 +1,16 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ + //insert to the front +func reversePrint(head *ListNode) []int { + res := []int{} + for head != nil { + res = append([]int{head.Val}, res...) + head = head.Next + } + return res +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" index b298a761d0364..88b0a55879cd7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" @@ -136,7 +136,40 @@ var buildTree = function(preorder, inorder) { }; ``` +### Go + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(preorder []int, inorder []int) *TreeNode { + return helper(preorder, inorder, 0, 0, len(preorder)-1) +} + +func helper(preorder, inorder []int, index, start, end int) *TreeNode { + if start > end { + return nil + } + root := &TreeNode{Val:preorder[index]} + j := start + for j < end && preorder[index] != inorder[j] { + j++ + } + root.Left = helper(preorder, inorder, index + 1, start, j - 1) + root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end) + return root +} +``` + + + ### ... + ``` ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.go" new file mode 100644 index 0000000000000..301abf7421877 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.go" @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(preorder []int, inorder []int) *TreeNode { + return helper(preorder, inorder, 0, 0, len(preorder)-1) +} + +func helper(preorder, inorder []int, index, start, end int) *TreeNode { + if start > end { + return nil + } + root := &TreeNode{Val:preorder[index]} + j := start + for j < end && preorder[index] != inorder[j] { + j++ + } + root.Left = helper(preorder, inorder, index + 1, start, j - 1) + root.Right = helper(preorder, inorder, index + 1 + j -start, j + 1, end) + return root +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index 3faf1c67687ed..94b2677bd0306 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -124,7 +124,49 @@ CQueue.prototype.deleteHead = function() { }; ``` +### Go + +```go +type CQueue struct { + Stack1 []int + Stack2 []int +} +// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶 +//否则,S1的元素从栈顶依次入S2,再从S2弹出 + +func Constructor() CQueue { + return CQueue{Stack1:[]int{},Stack2:[]int{}} +} + + +func (this *CQueue) AppendTail(value int) { + this.Stack1 = append(this.Stack1, value) +} + + +func (this *CQueue) DeleteHead() int { + if len(this.Stack1) == 0 && len(this.Stack2) == 0 { + return -1 + } + if len(this.Stack2) > 0 { + res := this.Stack2[len(this.Stack2)-1] + this.Stack2 = this.Stack2[0:len(this.Stack2)-1] + return res + } + for len(this.Stack1) > 0 { + this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1]) + this.Stack1 = this.Stack1[0:len(this.Stack1)-1] + } + res := this.Stack2[len(this.Stack2)-1] + this.Stack2 = this.Stack2[0:len(this.Stack2)-1] + return res +} +``` + + + ### ... + ``` ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.go" new file mode 100644 index 0000000000000..5eb2a09744de8 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.go" @@ -0,0 +1,34 @@ +type CQueue struct { + Stack1 []int + Stack2 []int +} +// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2 +//再从S2弹出 + +func Constructor() CQueue { + return CQueue{Stack1:[]int{},Stack2:[]int{}} +} + + +func (this *CQueue) AppendTail(value int) { + this.Stack1 = append(this.Stack1, value) +} + + +func (this *CQueue) DeleteHead() int { + if len(this.Stack1) == 0 && len(this.Stack2) == 0 { + return -1 + } + if len(this.Stack2) > 0 { + res := this.Stack2[len(this.Stack2)-1] + this.Stack2 = this.Stack2[0:len(this.Stack2)-1] + return res + } + for len(this.Stack1) > 0 { + this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1]) + this.Stack1 = this.Stack1[0:len(this.Stack1)-1] + } + res := this.Stack2[len(this.Stack2)-1] + this.Stack2 = this.Stack2[0:len(this.Stack2)-1] + return res +} \ No newline at end of file