邓心一 преди 7 години
родител
ревизия
f428cbb400
променени са 40 файла, в които са добавени 779 реда и са изтрити 266 реда
  1. 1 1
      easy/1.go
  2. 2 5
      easy/100.go
  3. 40 4
      easy/101.go
  4. 54 0
      easy/104.go
  5. 68 0
      easy/107.go
  6. 53 0
      easy/108.go
  7. 67 0
      easy/110.go
  8. 62 0
      easy/111.go
  9. 56 0
      easy/112.go
  10. 24 0
      easy/118.go
  11. 24 0
      easy/119.go
  12. 27 0
      easy/121.go
  13. 20 21
      easy/13.go
  14. 26 7
      easy/14.go
  15. 0 42
      easy/2.go
  16. 24 7
      easy/20.go
  17. 39 17
      easy/21.go
  18. 0 0
      easy/26.go
  19. 14 0
      easy/27.go
  20. 0 0
      easy/28.go
  21. 0 14
      easy/3.go
  22. 0 0
      easy/35.go
  23. 26 0
      easy/38.go
  24. 0 25
      easy/4.go
  25. 0 30
      easy/5.go
  26. 11 0
      easy/53.go
  27. 0 0
      easy/58.go
  28. 0 29
      easy/6.go
  29. 0 0
      easy/66.go
  30. 0 0
      easy/67.go
  31. 0 0
      easy/69.go
  32. 37 39
      easy/7.go
  33. 12 0
      easy/70.go
  34. 27 0
      easy/83.go
  35. 10 10
      easy/9.go
  36. 0 15
      easy/starter
  37. 35 0
      helper.go
  38. 20 0
      medium/12.go
  39. 0 0
      medium/2.go
  40. 0 0
      medium/3.go

+ 1 - 1
easy/1.go

@@ -17,6 +17,6 @@ func twoSum(nums []int, target int) []int {
 	return []int{-1, -1}
 }
 
-func test1() {
+func main() {
 	fmt.Println(twoSum([]int{1, 2, 5, 6, 7}, 11))
 }

+ 2 - 5
easy/100.go

@@ -21,12 +21,9 @@ import (
  */
 func isSameTree(p *TreeNode, q *TreeNode) bool {
 	// leaf node, eq
-	if p == nil && q == nil {
-		return true
-	}
-	// one of p/q is nil, ne
+	// one of p/q is not nil, ne
 	if p == nil || q == nil {
-		return false
+		return p == q
 	}
 	if p.Val != q.Val {
 		return false

+ 40 - 4
easy/101.go

@@ -11,8 +11,14 @@ type TreeNode struct {
 	Right *TreeNode
 }
 
-func isSymmetricIter() bool {
-
+func isSymmetricIter(lNode *TreeNode, rNode *TreeNode) bool {
+	if lNode == nil || rNode == nil {
+		return lNode == rNode
+	}
+	if lNode.Val != rNode.Val {
+		return false
+	}
+	return isSymmetricIter(lNode.Left, rNode.Right) && isSymmetricIter(lNode.Right, rNode.Left)
 }
 
 /**
@@ -24,9 +30,39 @@ func isSymmetricIter() bool {
  * }
  */
 func isSymmetric(root *TreeNode) bool {
-	return false
+	if root == nil {
+		return true
+	}
+	return isSymmetricIter(root.Left, root.Right)
 }
 
 func main() {
-	fmt.Println("")
+	/**
+	 * t1:  5
+	 *     / \
+	 *    1   4
+	 *       / \
+	 *      2   3
+	 */
+	t1l := TreeNode{1, nil, nil}
+	t1rl := TreeNode{2, nil, nil}
+	t1rr := TreeNode{3, nil, nil}
+	t1r := TreeNode{4, &t1rl, &t1rr}
+	t1 := &TreeNode{5, &t1l, &t1r}
+	/**
+	 * t2:   1
+	 *     /   \
+	 *    2     2
+	 *   / \   / \
+	 *  4   3 3   4
+	 */
+	t2ll := TreeNode{4, nil, nil}
+	t2lr := TreeNode{3, nil, nil}
+	t2l := TreeNode{2, &t2ll, &t2lr}
+	t2rl := TreeNode{3, nil, nil}
+	t2rr := TreeNode{4, nil, nil}
+	t2r := TreeNode{2, &t2rl, &t2rr}
+	t2 := &TreeNode{1, &t2l, &t2r}
+	fmt.Println(isSymmetric(t1))
+	fmt.Println(isSymmetric(t2))
 }

+ 54 - 0
easy/104.go

@@ -0,0 +1,54 @@
+package main
+
+import (
+	"fmt"
+)
+
+// TreeNode ...
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func maxInt(x, y int) int {
+	if x > y {
+		return x
+	}
+	return y
+}
+
+func maxDepthIter(curr *TreeNode, depth int) int {
+	if curr == nil {
+		return depth
+	}
+	return maxInt(maxDepthIter(curr.Left, depth+1), maxDepthIter(curr.Right, depth+1))
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func maxDepth(root *TreeNode) int {
+	return maxDepthIter(root, 0)
+}
+
+func main() {
+	/**
+	 * t1:  5
+	 *     / \
+	 *    1   4
+	 *       / \
+	 *      2   3
+	 */
+	t1l := TreeNode{1, nil, nil}
+	t1rl := TreeNode{2, nil, nil}
+	t1rr := TreeNode{3, nil, nil}
+	t1r := TreeNode{4, &t1rl, &t1rr}
+	t1 := &TreeNode{5, &t1l, &t1r}
+	fmt.Println(maxDepth(t1))
+}

+ 68 - 0
easy/107.go

@@ -0,0 +1,68 @@
+package main
+
+import (
+	"fmt"
+)
+
+// TreeNode ...
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func reverseArr(arr *[][]int) {
+	for i, j := 0, len(*arr)-1; i < j; i, j = i+1, j-1 {
+		(*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
+	}
+}
+
+func levelOrderBottomIter(curr *TreeNode, depth int, arr *[][]int) {
+	if curr == nil {
+		return
+	}
+	if curr.Left != nil {
+		levelOrderBottomIter(curr.Left, depth+1, arr)
+	}
+	if curr.Right != nil {
+		levelOrderBottomIter(curr.Right, depth+1, arr)
+	}
+	for len(*arr) < depth+1 {
+		layer := make([]int, 0)
+		*arr = append(*arr, layer)
+	}
+	(*arr)[depth] = append((*arr)[depth], curr.Val)
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func levelOrderBottom(root *TreeNode) [][]int {
+	arr := make([][]int, 0)
+	levelOrderBottomIter(root, 0, &arr)
+	reverseArr(&arr)
+	return arr
+}
+
+func main() {
+	/**
+	 * t1:  5
+	 *     / \
+	 *    1   4
+	 *       / \
+	 *      2   3
+	 */
+	t1l := TreeNode{1, nil, nil}
+	t1rl := TreeNode{2, nil, nil}
+	t1rr := TreeNode{3, nil, nil}
+	t1r := TreeNode{4, &t1rl, &t1rr}
+	t1 := &TreeNode{5, &t1l, &t1r}
+	t2 := &TreeNode{9, nil, nil}
+	fmt.Println(levelOrderBottom(t1))
+	fmt.Println(levelOrderBottom(t2))
+}

+ 53 - 0
easy/108.go

@@ -0,0 +1,53 @@
+package main
+
+import (
+	"fmt"
+)
+
+// print tree
+func printTree(root *TreeNode) {
+	if root == nil {
+		return
+	}
+	fmt.Print(root.Val, " ")
+	if root.Left != nil {
+		printTree(root.Left)
+	}
+	if root.Right != nil {
+		printTree(root.Right)
+	}
+}
+
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func sortedArrayToBST(nums []int) *TreeNode {
+	if len(nums) == 0 {
+		return nil
+	}
+	mid := len(nums) / 2
+	root := TreeNode{nums[mid], nil, nil}
+	// only one element in nums, return TreeNode{ele, nil, nil}
+	if mid == 0 {
+		return &root
+	}
+	root.Left = sortedArrayToBST(nums[:mid])
+	root.Right = sortedArrayToBST(nums[mid+1:])
+	return &root
+}
+
+func main() {
+	t1 := sortedArrayToBST([]int{-10, -3, 0, 5, 9})
+	printTree(t1)
+}

+ 67 - 0
easy/110.go

@@ -0,0 +1,67 @@
+package main
+
+import (
+	"fmt"
+)
+
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func maxInt(x, y int) int {
+	if x > y {
+		return x
+	}
+	return y
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+
+func treeDepth(root *TreeNode, depth int) int {
+	if root == nil {
+		return depth
+	}
+	return maxInt(treeDepth(root.Left, depth+1), treeDepth(root.Right, depth+1))
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func isBalanced(root *TreeNode) bool {
+	if root == nil {
+		return true
+	}
+	if abs(treeDepth(root.Left, 0)-treeDepth(root.Right, 0)) > 1 {
+		return false
+	}
+	return isBalanced(root.Left) && isBalanced(root.Right)
+}
+
+func main() {
+	/**
+	 * t1:  5
+	 *     / \
+	 *    1   4
+	 *       / \
+	 *      2   3
+	 */
+	t1l := TreeNode{1, nil, nil}
+	t1rl := TreeNode{2, nil, nil}
+	t1rr := TreeNode{3, nil, nil}
+	t1r := TreeNode{4, &t1rl, &t1rr}
+	t1 := &TreeNode{5, &t1l, &t1r}
+	fmt.Println(isBalanced(t1))
+	fmt.Println(isBalanced(nil))
+}

+ 62 - 0
easy/111.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+)
+
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}
+
+func minDepthIter(root *TreeNode, depth int) int {
+	if root.Left == nil && root.Right == nil {
+		return depth
+	}
+	if root.Left == nil {
+		return minDepthIter(root.Right, depth+1)
+	}
+	if root.Right == nil {
+		return minDepthIter(root.Left, depth+1)
+	}
+	return minInt(minDepthIter(root.Left, depth+1), minDepthIter(root.Right, depth+1))
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func minDepth(root *TreeNode) int {
+	if root == nil {
+		return 0
+	}
+	return minDepthIter(root, 1)
+}
+
+func main() {
+	/**
+	 * t1:  5
+	 *       \
+	 *        4
+	 *       / \
+	 *      2   3
+	 */
+	t1rl := TreeNode{2, nil, nil}
+	t1rr := TreeNode{3, nil, nil}
+	t1r := TreeNode{4, &t1rl, &t1rr}
+	t1 := &TreeNode{5, nil, &t1r}
+	fmt.Println(minDepth(t1))
+	fmt.Println(minDepth(nil))
+}

+ 56 - 0
easy/112.go

@@ -0,0 +1,56 @@
+package main
+
+import (
+	"fmt"
+)
+
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+// important!! "leaf node"
+func hasPathSumIter(root *TreeNode, sum int, tmpSum int) bool {
+	if root.Left == nil && root.Right == nil {
+		return tmpSum+root.Val == sum
+	}
+	if root.Left == nil {
+		return hasPathSumIter(root.Right, sum, tmpSum+root.Val)
+	}
+	if root.Right == nil {
+		return hasPathSumIter(root.Left, sum, tmpSum+root.Val)
+	}
+	return hasPathSumIter(root.Left, sum, tmpSum+root.Val) || hasPathSumIter(root.Right, sum, tmpSum+root.Val)
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func hasPathSum(root *TreeNode, sum int) bool {
+	if root == nil {
+		return false
+	}
+	return hasPathSumIter(root, sum, 0)
+}
+
+func main() {
+	/**
+	 * t1:  5
+	 *     / \
+	 *    1   4
+	 *       / \
+	 *      2   3
+	 */
+	t1r := TreeNode{4, nil, nil}
+	t1 := &TreeNode{5, nil, &t1r}
+	fmt.Println(hasPathSum(t1, 11))
+	fmt.Println(hasPathSum(t1, 5))
+	fmt.Println(hasPathSum(t1, 7))
+	fmt.Println(hasPathSum(nil, 0))
+}

+ 24 - 0
easy/118.go

@@ -0,0 +1,24 @@
+package main
+
+import (
+	"fmt"
+)
+
+func generate(numRows int) [][]int {
+	res := make([][]int, 0)
+	for i := 0; i < numRows; i++ {
+		res = append(res, make([]int, i+1))
+		for j := 0; j < i+1; j++ {
+			if j == 0 || j == i {
+				res[i][j] = 1
+				continue
+			}
+			res[i][j] = res[i-1][j-1] + res[i-1][j]
+		}
+	}
+	return res
+}
+
+func main() {
+	fmt.Println(generate(30))
+}

+ 24 - 0
easy/119.go

@@ -0,0 +1,24 @@
+package main
+
+import (
+	"fmt"
+)
+
+// O(k) space usage
+func getRow(rowIndex int) []int {
+	res := [2][]int{make([]int, rowIndex+1), make([]int, rowIndex+1)}
+	for i := 0; i < rowIndex+1; i++ {
+		for j := 0; j < i+1; j++ {
+			if j == 0 || i == j {
+				res[i&1][j] = 1
+				continue
+			}
+			res[i&1][j] = res[i&1^1][j-1] + res[i&1^1][j]
+		}
+	}
+	return res[rowIndex&1]
+}
+
+func main() {
+	fmt.Println(getRow(3))
+}

+ 27 - 0
easy/121.go

@@ -0,0 +1,27 @@
+package main
+
+import (
+	"fmt"
+)
+
+func maxProfit(prices []int) int {
+	cost, profit := 1<<31-1, 0
+	for _, v := range prices {
+		if v < cost {
+			cost = v
+		}
+		if v-cost > profit {
+			profit = v - cost
+		}
+	}
+	return profit
+}
+
+func main() {
+	a1 := []int{1, 2, 3, 4, 5}
+	a2 := []int{5, 4, 3, 2, 1}
+	a3 := []int{7, 4, 3, 8, 1}
+	fmt.Println(maxProfit(a1))
+	fmt.Println(maxProfit(a2))
+	fmt.Println(maxProfit(a3))
+}

+ 20 - 21
easy/13.go

@@ -1,26 +1,25 @@
 package main
 
-// iterator of countAndSay function
-func casIter(last string, n int) string {
-	if n == 1 {
-		return last
-	}
-	next := make([]rune, 0)
-	curr := last[0]
-	cnt := 1
-	for i := 1; i < len(last); i++ {
-		if curr != last[i] {
-			next = append(next, rune(cnt+'0'), rune(curr))
-			curr = last[i]
-			cnt = 1
-		} else {
-			cnt++
-		}
-	}
-	next = append(next, rune(cnt+'0'), rune(curr))
-	return casIter(string(next), n-1)
+var mRoman = map[rune]int{
+	'I': 1,
+	'V': 5,
+	'X': 10,
+	'L': 50,
+	'C': 100,
+	'D': 500,
+	'M': 1000,
 }
 
-func countAndSay(n int) string {
-	return casIter("1", n)
+func romanToInt(s string) int {
+	res := 0
+	last := 1000
+	for _, v := range s {
+		// IV --> V - I
+		if last < mRoman[v] {
+			res -= 2 * last
+		}
+		res += mRoman[v]
+		last = mRoman[v]
+	}
+	return res
 }

+ 26 - 7
easy/14.go

@@ -1,11 +1,30 @@
 package main
 
-// important. maxLeft: maximum of sum(nums[x], nums[i])
-func maxSubArray(nums []int) int {
-	max, maxLeft := nums[0], nums[0]
-	for i := 1; i < len(nums); i++ {
-		maxLeft = maxInt(maxLeft+nums[i], nums[i])
-		max = maxInt(max, maxLeft)
+func longestCommonPrefix(strs []string) string {
+	res := make([]rune, 0)
+	// spacial case
+	if len(res) == 0 {
+		return ""
 	}
-	return max
+	cnt := len(strs[0])
+	// find minimum length of strings
+	for _, v := range strs {
+		if cnt > len(v) {
+			cnt = len(v)
+		}2
+	}
+	// vertical serach
+	// [0 _] [0 _] [0 _] --> [_ 1] [_ 1] [_ 0] --> [0]
+	for i := 0; i < cnt; i++ {
+		curr := strs[0][i]
+		for _, v := range strs {
+			if curr != v[i] {
+				cnt = -1
+			}
+		}
+		if cnt != -1 {
+			res = append(res, rune(curr))
+		}
+	}
+	return string(res)
 }

+ 0 - 42
easy/2.go

@@ -1,42 +0,0 @@
-package main
-
-import (
-	"math"
-)
-
-func reverseOld(x int) int {
-	stack := make([]int, 0)
-	neg := false
-	if x < 0 {
-		neg = true
-		x = -x
-	}
-	for remain := x % 10; x > 0; remain = x % 10 {
-		stack = append(stack, remain)
-		x /= 10
-	}
-	res := 0
-	for i, v := range stack {
-		res += v * int(math.Pow10(len(stack)-i-1))
-	}
-	if res > 1<<31-1 || res < -1<<31 {
-		return 0
-	}
-	if neg {
-		return -res
-	}
-	return res
-}
-
-func reverse(x int) int {
-	res := int64(0)
-	for ; x != 0; x /= 10 {
-		res = res*10 + int64(x%10)
-		// overflow: -2^31 ~ 2^31-1
-		// if res > 1 << 31 - 1 || res < -1 << 31 { return 0 }
-		if res < math.MinInt32 || res > math.MaxInt32 {
-			return 0
-		}
-	}
-	return int(res)
-}

+ 24 - 7
easy/20.go

@@ -1,12 +1,29 @@
 package main
 
-func climbStairsIter(n, x, y int) int {
-	if n == 1 {
-		return y
-	}
-	return climbStairsIter(n-1, x+y, x)
+var mBrac = map[rune]rune{
+	'}': '{',
+	']': '[',
+	')': '(',
 }
 
-func climbStairs(n int) int {
-	return climbStairsIter(n, 2, 1)
+func isValid(s string) bool {
+	// store all brackets
+	slice := make([]rune, 0)
+	for _, v := range s {
+		if len(slice) == 0 {
+			slice = append(slice, v)
+			continue
+		}
+		// not match
+		if slice[len(slice)-1] != mBrac[v] {
+			slice = append(slice, v)
+			// match
+		} else {
+			slice = slice[:len(slice)-1]
+		}
+	}
+	if len(slice) != 0 {
+		return false
+	}
+	return true
 }

+ 39 - 17
easy/21.go

@@ -1,27 +1,49 @@
 package main
 
-/**
- * Definition for singly-linked list.
- * type ListNode struct {
- *     Val int
- *     Next *ListNode
- * }
- */
-// lots of pits!! [] [1] [1 1 1]
-func deleteDuplicates(head *ListNode) *ListNode {
-	if head == nil {
+// ListNode ...
+type ListNode struct {
+	Val  int
+	Next *ListNode
+}
+
+func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
+	// special cases: [] []; [0] []; [] [0]
+	if l1 == nil && l2 == nil {
 		return nil
 	}
-	if head.Next == nil {
-		return head
+	if l1 == nil && l2 != nil {
+		return l2
+	}
+	if l1 != nil && l2 == nil {
+		return l1
 	}
-	curr := head
-	for curr != nil && curr.Next != nil {
-		if curr.Next.Val == curr.Val {
-			curr.Next = curr.Next.Next
+	var curr *ListNode
+	// decide which one is head
+	if l1.Val < l2.Val {
+		curr = l1
+		l1 = l1.Next
+	} else {
+		curr = l2
+		l2 = l2.Next
+	}
+	head := curr
+	// merge 2 list until one of them is []
+	for l1 != nil && l2 != nil {
+		if l1.Val < l2.Val {
+			curr.Next = l1
+			curr = l1
+			l1 = l1.Next
 		} else {
-			curr = curr.Next
+			curr.Next = l2
+			curr = l2
+			l2 = l2.Next
 		}
 	}
+	// add tail
+	if l1 != nil {
+		curr.Next = l1
+	} else {
+		curr.Next = l2
+	}
 	return head
 }

+ 0 - 0
easy/8.go → easy/26.go


+ 14 - 0
easy/27.go

@@ -0,0 +1,14 @@
+package main
+
+// in-palce
+func removeElement(nums []int, val int) int {
+	// index of the last element in array
+	last := -1
+	for curr := 0; curr < len(nums); curr++ {
+		if nums[curr] != val {
+			nums[last+1] = nums[curr]
+			last++
+		}
+	}
+	return last + 1
+}

+ 0 - 0
easy/10.go → easy/28.go


+ 0 - 14
easy/3.go

@@ -1,14 +0,0 @@
-package main
-
-func isPalindrome(x int) bool {
-	// -1 --> -1-, impossible
-	if x < 0 {
-		return false
-	}
-	// judge if reverse(x) == x
-	res, y := 0, x
-	for ; x != 0; x /= 10 {
-		res = res*10 + x%10
-	}
-	return res == y
-}

+ 0 - 0
easy/12.go → easy/35.go


+ 26 - 0
easy/38.go

@@ -0,0 +1,26 @@
+package main
+
+// iterator of countAndSay function
+func casIter(last string, n int) string {
+	if n == 1 {
+		return last
+	}
+	next := make([]rune, 0)
+	curr := last[0]
+	cnt := 1
+	for i := 1; i < len(last); i++ {
+		if curr != last[i] {
+			next = append(next, rune(cnt+'0'), rune(curr))
+			curr = last[i]
+			cnt = 1
+		} else {
+			cnt++
+		}
+	}
+	next = append(next, rune(cnt+'0'), rune(curr))
+	return casIter(string(next), n-1)
+}
+
+func countAndSay(n int) string {
+	return casIter("1", n)
+}

+ 0 - 25
easy/4.go

@@ -1,25 +0,0 @@
-package main
-
-var mRoman = map[rune]int{
-	'I': 1,
-	'V': 5,
-	'X': 10,
-	'L': 50,
-	'C': 100,
-	'D': 500,
-	'M': 1000,
-}
-
-func romanToInt(s string) int {
-	res := 0
-	last := 1000
-	for _, v := range s {
-		// IV --> V - I
-		if last < mRoman[v] {
-			res -= 2 * last
-		}
-		res += mRoman[v]
-		last = mRoman[v]
-	}
-	return res
-}

+ 0 - 30
easy/5.go

@@ -1,30 +0,0 @@
-package main
-
-func longestCommonPrefix(strs []string) string {
-	res := make([]rune, 0)
-	// spacial case
-	if len(res) == 0 {
-		return ""
-	}
-	cnt := len(strs[0])
-	// find minimum length of strings
-	for _, v := range strs {
-		if cnt > len(v) {
-			cnt = len(v)
-		}
-	}
-	// vertical serach
-	// [0 _] [0 _] [0 _] --> [_ 1] [_ 1] [_ 0] --> [0]
-	for i := 0; i < cnt; i++ {
-		curr := strs[0][i]
-		for _, v := range strs {
-			if curr != v[i] {
-				cnt = -1
-			}
-		}
-		if cnt != -1 {
-			res = append(res, rune(curr))
-		}
-	}
-	return string(res)
-}

+ 11 - 0
easy/53.go

@@ -0,0 +1,11 @@
+package main
+
+// important. maxLeft: maximum of sum(nums[x], nums[i])
+func maxSubArray(nums []int) int {
+	max, maxLeft := nums[0], nums[0]
+	for i := 1; i < len(nums); i++ {
+		maxLeft = maxInt(maxLeft+nums[i], nums[i])
+		max = maxInt(max, maxLeft)
+	}
+	return max
+}

+ 0 - 0
easy/15.go → easy/58.go


+ 0 - 29
easy/6.go

@@ -1,29 +0,0 @@
-package main
-
-var mBrac = map[rune]rune{
-	'}': '{',
-	']': '[',
-	')': '(',
-}
-
-func isValid(s string) bool {
-	// store all brackets
-	slice := make([]rune, 0)
-	for _, v := range s {
-		if len(slice) == 0 {
-			slice = append(slice, v)
-			continue
-		}
-		// not match
-		if slice[len(slice)-1] != mBrac[v] {
-			slice = append(slice, v)
-			// match
-		} else {
-			slice = slice[:len(slice)-1]
-		}
-	}
-	if len(slice) != 0 {
-		return false
-	}
-	return true
-}

+ 0 - 0
easy/17.go → easy/66.go


+ 0 - 0
easy/18.go → easy/67.go


+ 0 - 0
easy/19.go → easy/69.go


+ 37 - 39
easy/7.go

@@ -1,49 +1,47 @@
 package main
 
-// ListNode ...
-type ListNode struct {
-	Val  int
-	Next *ListNode
-}
+import (
+	"fmt"
+	"math"
+)
 
-func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
-	// special cases: [] []; [0] []; [] [0]
-	if l1 == nil && l2 == nil {
-		return nil
+func reverseOld(x int) int {
+	stack := make([]int, 0)
+	neg := false
+	if x < 0 {
+		neg = true
+		x = -x
 	}
-	if l1 == nil && l2 != nil {
-		return l2
+	for remain := x % 10; x > 0; remain = x % 10 {
+		stack = append(stack, remain)
+		x /= 10
 	}
-	if l1 != nil && l2 == nil {
-		return l1
+	res := 0
+	for i, v := range stack {
+		res += v * int(math.Pow10(len(stack)-i-1))
 	}
-	var curr *ListNode
-	// decide which one is head
-	if l1.Val < l2.Val {
-		curr = l1
-		l1 = l1.Next
-	} else {
-		curr = l2
-		l2 = l2.Next
+	if res > 1<<31-1 || res < -1<<31 {
+		return 0
 	}
-	head := curr
-	// merge 2 list until one of them is []
-	for l1 != nil && l2 != nil {
-		if l1.Val < l2.Val {
-			curr.Next = l1
-			curr = l1
-			l1 = l1.Next
-		} else {
-			curr.Next = l2
-			curr = l2
-			l2 = l2.Next
-		}
+	if neg {
+		return -res
 	}
-	// add tail
-	if l1 != nil {
-		curr.Next = l1
-	} else {
-		curr.Next = l2
+	return res
+}
+
+func reverse(x int) int {
+	res := int64(0)
+	for ; x != 0; x /= 10 {
+		res = res*10 + int64(x%10)
+		// overflow: -2^31 ~ 2^31-1
+		// if res > 1 << 31 - 1 || res < -1 << 31 { return 0 }
+		if res < math.MinInt32 || res > math.MaxInt32 {
+			return 0
+		}
 	}
-	return head
+	return int(res)
+}
+
+func main() {
+	fmt.Println(reverse(1234))
 }

+ 12 - 0
easy/70.go

@@ -0,0 +1,12 @@
+package main
+
+func climbStairsIter(n, x, y int) int {
+	if n == 1 {
+		return y
+	}
+	return climbStairsIter(n-1, x+y, x)
+}
+
+func climbStairs(n int) int {
+	return climbStairsIter(n, 2, 1)
+}

+ 27 - 0
easy/83.go

@@ -0,0 +1,27 @@
+package main
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+// lots of pits!! [] [1] [1 1 1]
+func deleteDuplicates(head *ListNode) *ListNode {
+	if head == nil {
+		return nil
+	}
+	if head.Next == nil {
+		return head
+	}
+	curr := head
+	for curr != nil && curr.Next != nil {
+		if curr.Next.Val == curr.Val {
+			curr.Next = curr.Next.Next
+		} else {
+			curr = curr.Next
+		}
+	}
+	return head
+}

+ 10 - 10
easy/9.go

@@ -1,14 +1,14 @@
 package main
 
-// in-palce
-func removeElement(nums []int, val int) int {
-	// index of the last element in array
-	last := -1
-	for curr := 0; curr < len(nums); curr++ {
-		if nums[curr] != val {
-			nums[last+1] = nums[curr]
-			last++
-		}
+func isPalindrome(x int) bool {
+	// -1 --> -1-, impossible
+	if x < 0 {
+		return false
 	}
-	return last + 1
+	// judge if reverse(x) == x
+	res, y := 0, x
+	for ; x != 0; x /= 10 {
+		res = res*10 + x%10
+	}
+	return res == y
 }

+ 0 - 15
easy/starter

@@ -1,15 +0,0 @@
-package main
-
-import (
-	"fmt"
-)
-
-type TreeNode struct {
-	Val   int
-	Left  *TreeNode
-	Right *TreeNode
-}
-
-func main() {
-	fmt.Println("")
-}

+ 35 - 0
helper.go

@@ -1,5 +1,27 @@
 package helper
 
+import (
+	"fmt"
+)
+
+type ListNode struct {
+	Val  int
+	Next *ListNode
+}
+
+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
@@ -23,3 +45,16 @@ func list2str(head *ListNode) string {
 	}
 	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, " ")
+}

+ 20 - 0
medium/12.go

@@ -0,0 +1,20 @@
+package main
+
+var mRoman = map[int]rune{
+	1:    'I',
+	5:    'V',
+	10:   'X',
+	50:   'L',
+	100:  'C',
+	500:  'D',
+	1000: 'M',
+}
+
+func intToRoman(num int) string {
+	res := make([]rune, 0)
+	return string(res)
+}
+
+func main() {
+
+}

+ 0 - 0
easy/11.go → medium/2.go


+ 0 - 0
easy/16.go → medium/3.go