Skip to content

Commit f2cfef0

Browse files
committed
week_02: id_29 homework
1 parent 18a4c5b commit f2cfef0

12 files changed

+297
-0
lines changed

Week_02/id_29/LeetCode_001_29.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func twoSum(nums []int, target int) []int {
4+
// m store value and index
5+
visited := make(map[int]int)
6+
7+
for currentIndex, v := range nums {
8+
// 找對應的元素
9+
counterPart := target - v
10+
11+
counterPartIndex, ok := visited[counterPart]
12+
if ok {
13+
// 在 map 找到直接回傳
14+
return []int{counterPartIndex, currentIndex}
15+
}
16+
// 找不到就把自己加入到 map 裡面
17+
visited[v] = currentIndex
18+
}
19+
20+
return []int{-1, -1}
21+
}

Week_02/id_29/LeetCode_003_29.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
import "strings"
4+
5+
func lengthOfLongestSubstring(s string) int {
6+
maxLength := 0
7+
right := 0
8+
curr := ""
9+
10+
for right < len(s) {
11+
char := string(s[right])
12+
if !strings.Contains(curr, char) {
13+
curr += char
14+
if len(curr) > maxLength {
15+
maxLength = len(curr)
16+
}
17+
right++
18+
} else {
19+
curr = curr[1:]
20+
}
21+
}
22+
23+
return maxLength
24+
}

Week_02/id_29/LeetCode_098_29.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
func isValidBST(root *TreeNode) bool {
4+
return rec(root, nil, nil)
5+
}
6+
7+
func rec(node *TreeNode, min, max *int) bool {
8+
if node == nil {
9+
return true
10+
}
11+
12+
if min != nil && node.Val <= *min || max != nil && node.Val >= *max {
13+
return false
14+
}
15+
16+
return rec(node.Left, min, &node.Val) && rec(node.Right, &node.Val, max)
17+
}

Week_02/id_29/LeetCode_100_29.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func isSymmetric(root *TreeNode) bool {
4+
if root == nil {
5+
return true
6+
}
7+
8+
return ish(root.Left, root.Right)
9+
}
10+
11+
func ish(l, r *TreeNode) bool {
12+
if l == nil || r == nil {
13+
return l == r
14+
}
15+
16+
if l.Val != r.Val {
17+
return false
18+
}
19+
20+
return ish(l.Left, r.Right) && ish(l.Right, r.Left)
21+
}

Week_02/id_29/LeetCode_102_29.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
func levelOrder(root *TreeNode) [][]int {
4+
var ret [][]int
5+
6+
level := 1
7+
helper(root, level, &ret)
8+
9+
return ret
10+
}
11+
12+
func helper(root *TreeNode, level int, ret *[][]int) {
13+
if root == nil {
14+
return
15+
}
16+
17+
if len(*ret) < level {
18+
*ret = append(*ret, []int{root.Val})
19+
} else {
20+
(*ret)[level-1] = append((*ret)[level-1], root.Val)
21+
}
22+
23+
helper(root.Left, level+1, ret)
24+
helper(root.Right, level+1, ret)
25+
}

Week_02/id_29/LeetCode_103_29.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
func zigzagLevelOrder(root *TreeNode) [][]int {
4+
var result [][]int
5+
6+
vector := 1
7+
stack := []*TreeNode{root}
8+
9+
for len(stack) > 0 {
10+
var tmp []*TreeNode
11+
var levelVal []int
12+
for i := len(stack) - 1; i >= 0; i-- {
13+
node := stack[i]
14+
if node == nil {
15+
continue
16+
}
17+
if vector == 1 {
18+
tmp = append(tmp, node.Left, node.Right)
19+
} else {
20+
tmp = append(tmp, node.Right, node.Left)
21+
}
22+
levelVal = append(levelVal, node.Val)
23+
}
24+
vector = -vector
25+
stack = tmp
26+
if len(levelVal) > 0 {
27+
result = append(result, levelVal)
28+
}
29+
}
30+
31+
return result
32+
}

Week_02/id_29/LeetCode_111_29.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode
2+
3+
func minDepth(root *TreeNode) int {
4+
if root == nil {
5+
return 0
6+
}
7+
left := minDepth(root.Left)
8+
right := minDepth(root.Right)
9+
if left == 0 || right == 0 {
10+
return 1 + max(left, right)
11+
}
12+
return 1 + min(left, right)
13+
}
14+
15+
func min(x, y int) int {
16+
if x >= y {
17+
return y
18+
} else {
19+
return x
20+
}
21+
}
22+
23+
func max(x, y int) int {
24+
if x <= y {
25+
return y
26+
} else {
27+
return x
28+
}
29+
}

Week_02/id_29/LeetCode_236_29.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
4+
if root == nil {
5+
return nil
6+
}
7+
if root == p || root == q {
8+
return root
9+
}
10+
left := lowestCommonAncestor(root.Left, p, q)
11+
right := lowestCommonAncestor(root.Right, p, q)
12+
if left != nil && right != nil {
13+
return root
14+
}
15+
if left == nil {
16+
return right
17+
}
18+
return left
19+
}

Week_02/id_29/LeetCode_242_29.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package leetcode
2+
3+
func isAnagram(s string, t string) bool {
4+
if len(s) != len(t) {
5+
return false
6+
}
7+
8+
mapA := buildCharMap(s)
9+
mapB := buildCharMap(t)
10+
11+
for key, Acount := range mapA {
12+
Bcount, ok := mapB[key]
13+
if !ok {
14+
return false
15+
}
16+
if Acount != Bcount {
17+
return false
18+
}
19+
}
20+
21+
return true
22+
}
23+
24+
func buildCharMap(s string) map[rune]int {
25+
m := make(map[rune]int)
26+
27+
for _, char := range s {
28+
if _, ok := m[char]; ok {
29+
m[char]++
30+
} else {
31+
m[char] = 1
32+
}
33+
}
34+
35+
return m
36+
}

Week_02/id_29/LeetCode_692_29.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
type entry struct {
6+
word string
7+
frequence int
8+
}
9+
10+
type freWords []*entry
11+
12+
func topKFrequent(words []string, k int) []string {
13+
count := make(map[string]int, len(words))
14+
for _, w := range words {
15+
count[w]++
16+
}
17+
18+
fw := make(freWords, 0, len(count))
19+
for w, c := range count {
20+
fw = append(fw, &entry{
21+
word: w,
22+
frequence: c,
23+
})
24+
}
25+
26+
sort.Slice(fw, func(i, j int) bool {
27+
if fw[i].frequence == fw[j].frequence {
28+
return fw[i].word < fw[j].word
29+
}
30+
return fw[i].frequence > fw[j].frequence
31+
})
32+
33+
res := make([]string, k)
34+
for i := 0; i < k; i++ {
35+
res[i] = fw[i].word
36+
}
37+
38+
return res
39+
}

Week_02/id_29/LeetCode_938_29.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
func rangeSumBST(root *TreeNode, L int, R int) int {
4+
if root == nil {
5+
return 0
6+
}
7+
8+
sum := 0
9+
10+
switch {
11+
case root.Val < L:
12+
sum = rangeSumBST(root.Right, L, R)
13+
case R < root.Val:
14+
sum = rangeSumBST(root.Left, L, R)
15+
default:
16+
sum += root.Val
17+
sum += rangeSumBST(root.Left, L, R)
18+
sum += rangeSumBST(root.Right, L, R)
19+
}
20+
21+
return sum
22+
}

Week_02/id_29/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
type TreeNode struct {
9+
Val int
10+
Left *TreeNode
11+
Right *TreeNode
12+
}

0 commit comments

Comments
 (0)