dengxinyi hace 6 años
padre
commit
500b3bcc79
Se han modificado 14 ficheros con 434 adiciones y 45 borrados
  1. 1 1
      easy/helper.go
  2. 62 0
      hard/helper.go
  3. 57 0
      medium/102.go
  4. 53 0
      medium/103.go
  5. 0 7
      medium/11.go
  6. 0 7
      medium/16.go
  7. 0 16
      medium/19.go
  8. 0 7
      medium/3.go
  9. 29 7
      medium/91.go
  10. 45 0
      medium/92.go
  11. 42 0
      medium/93.go
  12. 64 0
      medium/94.go
  13. 19 0
      medium/95.go
  14. 62 0
      medium/helper.go

+ 1 - 1
helper.go → easy/helper.go

@@ -1,4 +1,4 @@
-package helper
+package main
 
 import (
 	"fmt"

+ 62 - 0
hard/helper.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+)
+
+// ListNode ...
+type ListNode struct {
+	Val  int
+	Next *ListNode
+}
+
+// TreeNode ...
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+
+func maxInt(x, y int) int {
+	if x > y {
+		return x
+	}
+	return y
+}
+
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}
+
+func list2str(head *ListNode) string {
+	curr := head
+	str := make([]rune, 0)
+	for curr != nil {
+		str = append(str, rune(curr.Val+'0'))
+		curr = curr.Next
+	}
+	return string(str)
+}
+
+func printTree(root *TreeNode) {
+	if root == nil {
+		return
+	}
+	if root.Left != nil {
+		printTree(root.Left)
+	}
+	if root.Right != nil {
+		printTree(root.Right)
+	}
+	fmt.Print(root.Val, " ")
+}

+ 57 - 0
medium/102.go

@@ -0,0 +1,57 @@
+package main
+
+// MyNode ...
+type MyNode struct {
+	Node  *TreeNode
+	Level int
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func levelOrder(root *TreeNode) [][]int {
+	result := [][]int{}
+	stack := []MyNode{}
+	curr := root
+	top := -1
+	level := 0
+	for curr != nil || top > -1 { // Inorder traversal
+		for curr != nil {
+			stack = append(stack, MyNode{curr, level})
+			top++
+			curr = curr.Left
+			level++
+		}
+		if top > -1 {
+			curr = stack[top].Node
+			level = stack[top].Level
+			stack = stack[:top]
+			top--
+			for len(result) <= level {
+				result = append(result, []int{})
+			}
+			result[level] = append(result[level], curr.Val)
+			curr = curr.Right
+			level++
+		}
+	}
+	return result
+}
+
+// func main() {
+// 	//        1
+// 	//       /
+// 	//      2
+// 	//     / \
+// 	//    3   4
+// 	n4 := TreeNode{4, nil, nil}
+// 	n3 := TreeNode{3, nil, nil}
+// 	n2 := TreeNode{2, &n3, &n4}
+// 	n1 := TreeNode{1, &n2, nil}
+// 	fmt.Println(levelOrder(&n1))
+// }

+ 53 - 0
medium/103.go

@@ -0,0 +1,53 @@
+package main
+
+import (
+	"fmt"
+)
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func zigzagLevelOrder(root *TreeNode) [][]int {
+	result := [][]int{}
+	if root == nil {
+		return result
+	}
+	zigzagLevelOrderHelper(root, 0, &result)
+	return result
+}
+
+func zigzagLevelOrderHelper(root *TreeNode, level int, result *[][]int) {
+	if root.Left != nil {
+		zigzagLevelOrderHelper(root.Left, level+1, result)
+	}
+	for len(*result) <= level {
+		*result = append(*result, []int{})
+	}
+	if level%2 == 0 {
+		(*result)[level] = append((*result)[level], root.Val)
+	} else {
+		(*result)[level] = append([]int{root.Val}, (*result)[level]...)
+	}
+	if root.Right != nil {
+		zigzagLevelOrderHelper(root.Right, level+1, result)
+	}
+}
+
+func main() {
+	//        1
+	//       / \
+	//      2   3
+	//     / \
+	//    4   5
+	n5 := TreeNode{5, nil, nil}
+	n4 := TreeNode{4, nil, nil}
+	n3 := TreeNode{3, nil, nil}
+	n2 := TreeNode{2, &n4, &n5}
+	n1 := TreeNode{1, &n2, &n3}
+	fmt.Println(zigzagLevelOrder(&n1))
+}

+ 0 - 7
medium/11.go

@@ -1,12 +1,5 @@
 package main
 
-func minInt(x, y int) int {
-	if x < y {
-		return x
-	}
-	return y
-}
-
 // 穷举
 func maxAreaOld(height []int) int {
 	n := len(height)

+ 0 - 7
medium/16.go

@@ -4,13 +4,6 @@ import (
 	"sort"
 )
 
-func abs(x int) int {
-	if x < 0 {
-		return -x
-	}
-	return x
-}
-
 // same as three sum
 func threeSumClosest(nums []int, target int) int {
 	sort.Ints(nums)

+ 0 - 16
medium/19.go

@@ -1,21 +1,5 @@
 package main
 
-// ListNode ...
-type ListNode struct {
-	Val  int
-	Next *ListNode
-}
-
-func list2str(head *ListNode) string {
-	curr := head
-	str := make([]rune, 0)
-	for curr != nil {
-		str = append(str, rune(curr.Val+'0'))
-		curr = curr.Next
-	}
-	return string(str)
-}
-
 /**
  * Definition for singly-linked list.
  * type ListNode struct {

+ 0 - 7
medium/3.go

@@ -1,12 +1,5 @@
 package main
 
-func maxInt(x, y int) int {
-	if x > y {
-		return x
-	}
-	return y
-}
-
 func lengthOfLongestSubstring(s string) int {
 	max := 0
 	for i := 0; i < len(s); i++ {

+ 29 - 7
medium/91.go

@@ -1,12 +1,34 @@
 package main
 
-import "fmt"
-
 func numDecodings(s string) int {
-	return 0
+	// just like the stair jumping problem,
+	// solution cnt = last cnt + last last cnt (under some condition)
+	strLen := len(s)
+	if strLen == 0 || s[0] == '0' {
+		return 0
+	} else if strLen == 1 {
+		return 1
+	}
+	solutionCnt := make([]int, strLen)
+	solutionCnt[0] = 1
+	if s[0] == '1' || (s[0] == '2' && s[1] <= '6') {
+		solutionCnt[1] = 1
+	}
+	if s[1] != '0' {
+		solutionCnt[1] += solutionCnt[0]
+	}
+	for i := 2; i < strLen; i++ {
+		if s[i-1] == '1' || (s[i-1] == '2' && s[i] <= '6') {
+			solutionCnt[i] += solutionCnt[i-2]
+		}
+		if s[i] != '0' {
+			solutionCnt[i] += solutionCnt[i-1]
+		}
+	}
+	return solutionCnt[strLen-1]
 }
 
-func main() {
-	str := "12432546"
-	fmt.Println(numDecodings(str))
-}
+// func main() {
+// 	str := "12345120"
+// 	fmt.Println(numDecodings(str))
+// }

+ 45 - 0
medium/92.go

@@ -0,0 +1,45 @@
+package main
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+// 1 <= m <= n <= len of list
+func reverseBetween(head *ListNode, m int, n int) *ListNode {
+	var pre *ListNode
+	curr, next := head, head.Next
+	for cnt := 1; next != nil; cnt++ {
+		if cnt == m {
+			beforeBeg, beg := pre, curr
+			for ; cnt != n; cnt++ {
+				curr.Next = pre
+				pre = curr
+				curr = next
+				next = next.Next
+			}
+			if beforeBeg != nil { // before beg -> end
+				beforeBeg.Next = curr
+			} else {
+				head = curr // head is end
+			}
+			curr.Next = pre // end -> pre
+			beg.Next = next // beg -> after end
+			break
+		}
+		pre = curr
+		curr = next
+		next = next.Next
+	}
+	return head
+}
+
+// func main() {
+// 	n3 := ListNode{3, nil}
+// 	n2 := ListNode{2, &n3}
+// 	n1 := ListNode{1, &n2}
+// 	n0 := ListNode{0, &n1}
+// 	fmt.Println(list2str(reverseBetween(&n0, 3, 4)))
+// }

+ 42 - 0
medium/93.go

@@ -0,0 +1,42 @@
+package main
+
+func isValidPart(part string) bool {
+	switch len(part) {
+	case 1:
+		return true
+	case 2:
+		return part[0] != '0'
+	case 3:
+		if part[0] != '2' {
+			return part[0] == '1'
+		}
+		return part[1] <= '4' ||
+			(part[1] == '5' && part[2] <= '5')
+	}
+	return false
+}
+
+func restoreIpAddresses(s string) []string {
+	result := []string{}
+	strLen := len(s)
+	if strLen < 4 || 12 < strLen {
+		return result
+	}
+	for i := 1; i < 4 && i < strLen-2; i++ {
+		for j := i + 1; j < i+4 && j < strLen-1; j++ {
+			for k := j + 1; k < j+4 && k < strLen; k++ {
+				if isValidPart(s[0:i]) && isValidPart(s[i:j]) &&
+					isValidPart(s[j:k]) && isValidPart(s[k:]) {
+					result = append(result, s[0:i]+"."+s[i:j]+"."+s[j:k]+"."+s[k:])
+				}
+			}
+		}
+	}
+	return result
+}
+
+// func main() {
+// 	fmt.Println(restoreIpAddresses("25525511135"))
+// 	fmt.Println(restoreIpAddresses("0000"))
+// 	fmt.Println(restoreIpAddresses("00000"))
+// }

+ 64 - 0
medium/94.go

@@ -0,0 +1,64 @@
+package main
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func inorderTraversal(root *TreeNode) []int {
+	result := []int{}
+	if root == nil {
+		return result
+	}
+	inorderTraversalHelper(root, &result)
+	return result
+}
+
+func inorderTraversalHelper(root *TreeNode, result *[]int) {
+	if root.Left != nil {
+		inorderTraversalHelper(root.Left, result)
+	}
+	*result = append(*result, root.Val)
+	if root.Right != nil {
+		inorderTraversalHelper(root.Right, result)
+	}
+}
+
+func inorderTraversalIter(root *TreeNode) []int {
+	result := []int{}
+	stack := []*TreeNode{}
+	curr := root
+	top := -1
+	for curr != nil || top > -1 {
+		for curr != nil {
+			// push
+			stack = append(stack, curr)
+			top++
+			curr = curr.Left
+		}
+		if top > -1 {
+			curr = stack[top]
+			// pop
+			stack = stack[:top]
+			top--
+			result = append(result, curr.Val)
+			curr = curr.Right
+		}
+	}
+	return result
+}
+
+// func main() {
+// 	//        1
+// 	//       /
+// 	//      2
+// 	//     / \
+// 	//    3  nil
+// 	n3 := TreeNode{3, nil, nil}
+// 	n2 := TreeNode{2, &n3, nil}
+// 	n1 := TreeNode{1, &n2, nil}
+// 	fmt.Println(inorderTraversalIter(&n1))
+// }

+ 19 - 0
medium/95.go

@@ -0,0 +1,19 @@
+package main
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func generateTrees(n int) []*TreeNode {
+	result := []*TreeNode{}
+
+	return result
+}
+
+// func main() {
+// 	fmt.Println(generateTrees(3))
+// }

+ 62 - 0
medium/helper.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+)
+
+// ListNode ...
+type ListNode struct {
+	Val  int
+	Next *ListNode
+}
+
+// TreeNode ...
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+
+func maxInt(x, y int) int {
+	if x > y {
+		return x
+	}
+	return y
+}
+
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}
+
+func list2str(head *ListNode) string {
+	curr := head
+	str := make([]rune, 0)
+	for curr != nil {
+		str = append(str, rune(curr.Val+'0'))
+		curr = curr.Next
+	}
+	return string(str)
+}
+
+func printTree(root *TreeNode) {
+	if root == nil {
+		return
+	}
+	if root.Left != nil {
+		printTree(root.Left)
+	}
+	if root.Right != nil {
+		printTree(root.Right)
+	}
+	fmt.Print(root.Val, " ")
+}