ソースを参照

split into multi files

邓心一 7 年 前
コミット
b8545e8bc3
29 ファイル変更567 行追加485 行削除
  1. 22 0
      .vscode/launch.json
  2. 13 9
      easy/1.go
  3. 13 15
      easy/10.go
  4. 60 0
      easy/100.go
  5. 32 0
      easy/101.go
  6. 36 43
      easy/11.go
  7. 10 13
      easy/12.go
  8. 19 23
      easy/13.go
  9. 7 13
      easy/14.go
  10. 6 6
      easy/15.go
  11. 20 19
      easy/16.go
  12. 12 16
      easy/17.go
  13. 46 44
      easy/18.go
  14. 4 10
      easy/19.go
  15. 32 32
      easy/2.go
  16. 6 11
      easy/20.go
  17. 15 16
      easy/21.go
  18. 0 32
      easy/22.go
  19. 10 8
      easy/3.go
  20. 20 24
      easy/4.go
  21. 27 27
      easy/5.go
  22. 23 27
      easy/6.go
  23. 42 41
      easy/7.go
  24. 14 20
      easy/8.go
  25. 36 0
      easy/88.go
  26. 9 15
      easy/9.go
  27. 0 11
      easy/leet.go
  28. 15 0
      easy/starter
  29. 18 10
      helper.go

+ 22 - 0
.vscode/launch.json

@@ -0,0 +1,22 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        
+        {
+            "name": "Launch",
+            "type": "go",
+            "request": "launch",
+            "mode": "debug",
+            "remotePath": "",
+            "port": 2345,
+            "host": "127.0.0.1",
+            "program": "${fileDirname}",
+            "env": {},
+            "args": [],
+            "showLog": true
+        }
+    ]
+}

+ 13 - 9
easy/1.go

@@ -1,18 +1,22 @@
 package main
 
 import (
-    "fmt"
+	"fmt"
 )
 
 func twoSum(nums []int, target int) []int {
 	var i, j, v int
-    for i, v = range nums {
-        for j = i+1; j < len(nums); j++ {
-            if v + nums[j] == target {
-                return []int{i, j}
-            }
-        }
-    }
-    // not found, [-1 -1]
+	for i, v = range nums {
+		for j = i + 1; j < len(nums); j++ {
+			if v+nums[j] == target {
+				return []int{i, j}
+			}
+		}
+	}
+	// not found, [-1 -1]
 	return []int{-1, -1}
 }
+
+func test1() {
+	fmt.Println(twoSum([]int{1, 2, 5, 6, 7}, 11))
+}

+ 13 - 15
easy/10.go

@@ -1,19 +1,17 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 // search needle in haystack
 func strStr(haystack string, needle string) int {
-    for i := 0; i <= len(haystack) - len(needle); i++ {
-        j := 0
-        for ; j < len(needle); j++ {
-            if haystack[i + j] != needle[j] { break }
-        }
-        if j == len(needle) { return i }
-    }
-    return -1
-}
+	for i := 0; i <= len(haystack)-len(needle); i++ {
+		j := 0
+		for ; j < len(needle); j++ {
+			if haystack[i+j] != needle[j] {
+				break
+			}
+		}
+		if j == len(needle) {
+			return i
+		}
+	}
+	return -1
+}

+ 60 - 0
easy/100.go

@@ -0,0 +1,60 @@
+package main
+
+import (
+	"fmt"
+)
+
+// TreeNode ...
+// 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 isSameTree(p *TreeNode, q *TreeNode) bool {
+	// leaf node, eq
+	if p == nil && q == nil {
+		return true
+	}
+	// one of p/q is nil, ne
+	if p == nil || q == nil {
+		return false
+	}
+	if p.Val != q.Val {
+		return false
+	}
+	// preoder traversal, DLR
+	return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
+}
+
+func test100() {
+	/**
+	 * 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: same as t1
+	 */
+	t2l := TreeNode{1, nil, nil}
+	t2rl := TreeNode{2, nil, nil}
+	t2rr := TreeNode{3, nil, nil}
+	t2r := TreeNode{4, &t2rl, &t2rr}
+	t2 := &TreeNode{5, &t2l, &t2r}
+	fmt.Println(isSameTree(t1, t2))
+}

+ 32 - 0
easy/101.go

@@ -0,0 +1,32 @@
+package main
+
+import (
+	"fmt"
+)
+
+// TreeNode ...
+type TreeNode struct {
+	Val   int
+	Left  *TreeNode
+	Right *TreeNode
+}
+
+func isSymmetricIter() bool {
+
+}
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func isSymmetric(root *TreeNode) bool {
+	return false
+}
+
+func main() {
+	fmt.Println("")
+}

+ 36 - 43
easy/11.go

@@ -1,12 +1,5 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 /**
  * Definition for singly-linked list.
  * type ListNode struct {
@@ -16,40 +9,40 @@ import (
  */
 // (3->2->1) + (2->1) = (5->3->1), 123 + 12 = 135
 func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
-    // get head of result list
-    head := &ListNode{(l1.Val + l2.Val) % 10, nil}
-    remain := int((l1.Val + l2.Val) / 10)
-    curr := head
-    l1 = l1.Next
-    l2 = l2.Next
-    // while l1 != [] and l2 != [], calculate l1 + l2
-    for l1 != nil && l2 != nil {
-        curr.Next = &ListNode{(l1.Val + l2.Val + remain) % 10, nil}
-        remain = int((l1.Val + l2.Val + remain) / 10)
-        curr = curr.Next
-        l1 = l1.Next
-        l2 = l2.Next
-    }
-    // l1 / l2 is nil 
-    if l1 != nil {
-        curr.Next = &ListNode{(l1.Val + remain) % 10, l1.Next}
-        remain = int((l1.Val + remain) / 10)
-    } else if l2 != nil {
-        curr.Next = &ListNode{(l2.Val + remain) % 10, l2.Next}
-        remain = int((l2.Val + remain) / 10)
-    }
-    curr = curr.Next
-    // add remainder
-    for remain != 0 {
-        if curr.Next != nil {
-            curr.Next.Val += remain
-            remain = int(curr.Next.Val / 10)
-            curr.Next.Val %= 10
-            curr = curr.Next
-        } else {
-            curr.Next = &ListNode{remain, nil}
-            remain = 0
-        }
-    }
-    return head
+	// get head of result list
+	head := &ListNode{(l1.Val + l2.Val) % 10, nil}
+	remain := int((l1.Val + l2.Val) / 10)
+	curr := head
+	l1 = l1.Next
+	l2 = l2.Next
+	// while l1 != [] and l2 != [], calculate l1 + l2
+	for l1 != nil && l2 != nil {
+		curr.Next = &ListNode{(l1.Val + l2.Val + remain) % 10, nil}
+		remain = int((l1.Val + l2.Val + remain) / 10)
+		curr = curr.Next
+		l1 = l1.Next
+		l2 = l2.Next
+	}
+	// l1 / l2 is nil
+	if l1 != nil {
+		curr.Next = &ListNode{(l1.Val + remain) % 10, l1.Next}
+		remain = int((l1.Val + remain) / 10)
+	} else if l2 != nil {
+		curr.Next = &ListNode{(l2.Val + remain) % 10, l2.Next}
+		remain = int((l2.Val + remain) / 10)
+	}
+	curr = curr.Next
+	// add remainder
+	for remain != 0 {
+		if curr.Next != nil {
+			curr.Next.Val += remain
+			remain = int(curr.Next.Val / 10)
+			curr.Next.Val %= 10
+			curr = curr.Next
+		} else {
+			curr.Next = &ListNode{remain, nil}
+			remain = 0
+		}
+	}
+	return head
 }

+ 10 - 13
easy/12.go

@@ -1,17 +1,14 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 func searchInsert(nums []int, target int) int {
-    idx := 0
-    for i, v := range nums {
-        if v == target { return i }
-        if v < target { idx = i + 1 }
-    }
-    return idx
+	idx := 0
+	for i, v := range nums {
+		if v == target {
+			return i
+		}
+		if v < target {
+			idx = i + 1
+		}
+	}
+	return idx
 }

+ 19 - 23
easy/13.go

@@ -1,30 +1,26 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 // 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)
+	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)
-}
+	return casIter("1", n)
+}

+ 7 - 13
easy/14.go

@@ -1,17 +1,11 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 // 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
-}
+	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
+}

+ 6 - 6
easy/15.go

@@ -2,12 +2,12 @@ package main
 
 import (
 	"strings"
-    "fmt"
-    "math"
 )
 
 func lengthOfLastWord(s string) int {
-    arr := strings.Fields(s)
-    if len(arr) == 0 { return 0 }
-    return len(arr[len(arr)-1])
-}
+	arr := strings.Fields(s)
+	if len(arr) == 0 {
+		return 0
+	}
+	return len(arr[len(arr)-1])
+}

+ 20 - 19
easy/16.go

@@ -1,24 +1,25 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
+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++ {
-        // a map stores all runes showed up after index i
-        m := map[byte]int{ s[i]: 1 }
-        for j := i + 1; j < len(s); j++ {
-            if m[s[j]] == 1 {
-                max = maxInt(len(m), max)
-                break
-            }
-            m[s[j]] = 1
-        }
-        max = maxInt(len(m), max)
-    }
-    return max
+	max := 0
+	for i := 0; i < len(s); i++ {
+		// a map stores all runes showed up after index i
+		m := map[byte]int{s[i]: 1}
+		for j := i + 1; j < len(s); j++ {
+			if m[s[j]] == 1 {
+				max = maxInt(len(m), max)
+				break
+			}
+			m[s[j]] = 1
+		}
+		max = maxInt(len(m), max)
+	}
+	return max
 }

+ 12 - 16
easy/17.go

@@ -1,20 +1,16 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 // just like "add two numbers"
 func plusOne(digits []int) []int {
-    i, remain := len(digits) - 1, 0
-    for digits[len(digits) - 1] = digits[len(digits) - 1] + 1; i >= 0; i-- {
-        digits[i] += remain
-        remain = int(digits[i] / 10)
-        digits[i] %= 10
-    }
-    // way to concat two slice: append(a, b...)
-    if remain == 1 { return append([]int{1}, digits...) }
-    return digits
-}
+	i, remain := len(digits)-1, 0
+	for digits[len(digits)-1] = digits[len(digits)-1] + 1; i >= 0; i-- {
+		digits[i] += remain
+		remain = int(digits[i] / 10)
+		digits[i] %= 10
+	}
+	// way to concat two slice: append(a, b...)
+	if remain == 1 {
+		return append([]int{1}, digits...)
+	}
+	return digits
+}

+ 46 - 44
easy/18.go

@@ -1,59 +1,61 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 // add binary
 func minInt(x, y int) int {
-    if x < y { return x }
-    return y
+	if x < y {
+		return x
+	}
+	return y
 }
 
 const one, zero = byte('1'), byte('0')
 
 func add3Bi(x, y, z byte) (sum, remain byte) {
-    switch x + y + z - 3 * zero {
-    case 0: return zero, zero
-    case 1: return one, zero
-    case 2: return zero, one
-    case 3: return one, one
-    default: return
-    }
+	switch x + y + z - 3*zero {
+	case 0:
+		return zero, zero
+	case 1:
+		return one, zero
+	case 2:
+		return zero, one
+	case 3:
+		return one, one
+	default:
+		return
+	}
 }
 
 func reverseStr(s string) string {
-    runes := []rune(s)
-    for from, to := 0, len(s) - 1; from < to; from, to = from + 1, to - 1 {
-        runes[from], runes[to] = runes[to], runes[from]
-    }
-    return string(runes)
+	runes := []rune(s)
+	for from, to := 0, len(s)-1; from < to; from, to = from+1, to-1 {
+		runes[from], runes[to] = runes[to], runes[from]
+	}
+	return string(runes)
 }
 
 func addBinary(a string, b string) string {
-    la, lb := len(a) - 1, len(b) - 1
-    sum, remain := zero, zero
-    res := make([]rune, 0)
-    for i := 0; i <= minInt(la, lb); i++ {
-         sum, remain = add3Bi(a[la - i], b[lb - i], remain)
-         res = append(res, rune(sum))
-    }
-    lc := 0
-    var c string
-    if la < lb {
-        c = b
-        lc = lb
-    } else {
-        c = a
-        lc = la
-    }
-    for i := minInt(la, lb) + 1; i <= lc; i++ {
-        sum, remain = add3Bi(zero, c[lc - i], remain)
-        res = append(res, rune(sum))
-    }
-    if remain == one { res = append(res, rune(one)) }
-    return reverseStr(string(res))
-}
+	la, lb := len(a)-1, len(b)-1
+	sum, remain := zero, zero
+	res := make([]rune, 0)
+	for i := 0; i <= minInt(la, lb); i++ {
+		sum, remain = add3Bi(a[la-i], b[lb-i], remain)
+		res = append(res, rune(sum))
+	}
+	lc := 0
+	var c string
+	if la < lb {
+		c = b
+		lc = lb
+	} else {
+		c = a
+		lc = la
+	}
+	for i := minInt(la, lb) + 1; i <= lc; i++ {
+		sum, remain = add3Bi(zero, c[lc-i], remain)
+		res = append(res, rune(sum))
+	}
+	if remain == one {
+		res = append(res, rune(one))
+	}
+	return reverseStr(string(res))
+}

+ 4 - 10
easy/19.go

@@ -1,14 +1,8 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 func mySqrt(x int) int {
-    var i int
-    for i = 0; i * i <= x; i++ { }
-    return i - 1
+	var i int
+	for i = 0; i*i <= x; i++ {
+	}
+	return i - 1
 }

+ 32 - 32
easy/2.go

@@ -1,42 +1,42 @@
 package main
 
 import (
-	"strings"
-    "fmt"
-    "math"
+	"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
+	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)
+	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)
 }

+ 6 - 11
easy/20.go

@@ -1,17 +1,12 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 func climbStairsIter(n, x, y int) int {
-    if n == 1 { return y }
-    return climbStairsIter(n - 1, x + y, x)
+	if n == 1 {
+		return y
+	}
+	return climbStairsIter(n-1, x+y, x)
 }
 
 func climbStairs(n int) int {
-    return climbStairsIter(n, 2, 1)
-}
+	return climbStairsIter(n, 2, 1)
+}

+ 15 - 16
easy/21.go

@@ -1,12 +1,5 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
 /**
  * Definition for singly-linked list.
  * type ListNode struct {
@@ -16,13 +9,19 @@ import (
  */
 // 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
+	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
 }

+ 0 - 32
easy/22.go

@@ -1,32 +0,0 @@
-package main
-
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
-func merge(nums1 []int, m int, nums2 []int, n int)  {
-    nums := make([]int, 0)
-    i, j := 0, 0
-    // pit: add the smaller one "once a time"
-    for i < m && j < n {
-        if nums1[i] < nums2[j] {
-            nums = append(nums, nums1[i])
-            i++
-        } else {
-            nums = append(nums, nums2[j])
-            j++
-        }
-    }
-    if i == m && j == n {
-    } else if i == m {
-        nums = append(nums, nums2[j:]...)
-    } else {
-        nums = append(nums, nums1[i:]...)
-    }
-    for i = 0; i < m + n; i++ {
-        nums1[i] = nums[i]
-    }
-}

+ 10 - 8
easy/3.go

@@ -1,12 +1,14 @@
 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
+	// -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
 }

+ 20 - 24
easy/4.go

@@ -1,29 +1,25 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-var m_roman = map[rune]int {
-    'I': 1,
-    'V': 5,
-    'X': 10,
-    'L': 50,
-    'C': 100,
-    'D': 500,
-    'M': 1000,
+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 < m_roman[v] { res -= 2 * last }
-        res += m_roman[v]
-        last = m_roman[v]
-    }
-    return res
-}
+	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
+}

+ 27 - 27
easy/5.go

@@ -1,30 +1,30 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 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_r := strs[0][i]
-        for _, v := range strs {
-            if (curr_r != v[i]) { cnt = -1 }
-        }
-        if cnt != -1 { 
-            res = append(res, rune(curr_r))
-        }
-    }
-    return string(res)
-}
+	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)
+}

+ 23 - 27
easy/6.go

@@ -1,33 +1,29 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-var m_brac = map[rune]rune {
-    '}': '{',
-    ']': '[',
-    ')': '(',
+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] != m_brac[v] {
-            slice = append(slice, v)
-        // match
-        } else {
-            slice = slice[:len(slice)-1]
-        }
-    }
-    if len(slice) != 0 { return false }
-    return true
+	// 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
 }

+ 42 - 41
easy/7.go

@@ -1,48 +1,49 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
+// ListNode ...
 type ListNode struct {
-    Val int
-    Next *ListNode
+	Val  int
+	Next *ListNode
 }
 
 func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
-    // special cases: [] []; [0] []; [] [0]
-    if l1 == nil && l2 == nil { return nil }
-    if l1 == nil && l2 != nil { return l2 }    
-    if l1 != nil && l2 == nil { return l1 }    
-    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.Next = l2
-            curr = l2
-            l2 = l2.Next
-        }
-    }
-    // add tail
-    if l1 != nil {
-        curr.Next = l1
-    } else {
-        curr.Next = l2
-    }
-    return head
+	// special cases: [] []; [0] []; [] [0]
+	if l1 == nil && l2 == nil {
+		return nil
+	}
+	if l1 == nil && l2 != nil {
+		return l2
+	}
+	if l1 != nil && l2 == nil {
+		return l1
+	}
+	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.Next = l2
+			curr = l2
+			l2 = l2.Next
+		}
+	}
+	// add tail
+	if l1 != nil {
+		curr.Next = l1
+	} else {
+		curr.Next = l2
+	}
+	return head
 }

+ 14 - 20
easy/8.go

@@ -1,24 +1,18 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
-
-
 // in-place
 func removeDuplicates(nums []int) int {
-    if len(nums) == 0 { return 0 }
-    // index of the last element in array
-    last := 0
-    for curr := 1; curr < len(nums); curr++ {
-        if nums[curr] != nums[last] {
-            nums[last + 1] = nums[curr]
-            last++
-        }
-    }
-    // size = last_index + 1
-    return last + 1
-}
+	if len(nums) == 0 {
+		return 0
+	}
+	// index of the last element in array
+	last := 0
+	for curr := 1; curr < len(nums); curr++ {
+		if nums[curr] != nums[last] {
+			nums[last+1] = nums[curr]
+			last++
+		}
+	}
+	// size = last_index + 1
+	return last + 1
+}

+ 36 - 0
easy/88.go

@@ -0,0 +1,36 @@
+package main
+
+import (
+	"fmt"
+)
+
+func merge(nums1 []int, m int, nums2 []int, n int) {
+	nums := make([]int, 0)
+	i, j := 0, 0
+	// pit: add the smaller one "once a time"
+	for i < m && j < n {
+		if nums1[i] < nums2[j] {
+			nums = append(nums, nums1[i])
+			i++
+		} else {
+			nums = append(nums, nums2[j])
+			j++
+		}
+	}
+	if i == m && j == n {
+	} else if i == m {
+		nums = append(nums, nums2[j:]...)
+	} else {
+		nums = append(nums, nums1[i:]...)
+	}
+	for i = 0; i < m+n; i++ {
+		nums1[i] = nums[i]
+	}
+}
+
+func test88() {
+	n1 := []int{1, 1, 2, 9, 0, 0, 0}
+	n2 := []int{2, 3, 3}
+	merge(n1, 4, n2, 3)
+	fmt.Println(n1)
+}

+ 9 - 15
easy/9.go

@@ -1,20 +1,14 @@
 package main
 
-import (
-	"strings"
-    "fmt"
-    "math"
-)
-
 // 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
+	// 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 - 11
easy/leet.go

@@ -1,11 +0,0 @@
-package main
-
-import (
-	// "strings"
-    "fmt"
-    // "math"
-)
-
-func main()  {
-    fmt.Println("123")
-}

+ 15 - 0
easy/starter

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

+ 18 - 10
helper.go

@@ -1,17 +1,25 @@
 package helper
 
-
 func maxInt(x, y int) int {
-    if x > y { return x }
-    return y
+	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)
+	curr := head
+	str := make([]rune, 0)
+	for curr != nil {
+		str = append(str, rune(curr.Val+'0'))
+		curr = curr.Next
+	}
+	return string(str)
 }