Skip to content

Commit 34590eb

Browse files
committed
add new
0 parents  commit 34590eb

12 files changed

+256
-0
lines changed

1. Two Sum.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
func twoSum(nums []int, target int) []int {
4+
m := make(map[int]int)
5+
for i := 0; i < len(nums); i++ {
6+
c, ok := m[nums[i]]
7+
if ok {
8+
r := []int{c, i}
9+
return r
10+
} else {
11+
m[target-nums[i]] = i
12+
}
13+
}
14+
r := []int{0, 0}
15+
return r
16+
}

101. Symmetric Tree.go

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

104. Maximum Depth of Binary Tree.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "math"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func maxDepth(root *TreeNode) int {
12+
if root == nil {
13+
return 0
14+
}
15+
l := maxDepth(root.Left)
16+
r := maxDepth(root.Right)
17+
return int(math.Max(float64(l), float64(r))) + 1
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func buildTree(preorder []int, inorder []int) *TreeNode {
10+
if len(preorder) == 0 {
11+
return nil
12+
}
13+
res := &TreeNode{
14+
Val: preorder[0],
15+
}
16+
if len(preorder) == 1 {
17+
return res
18+
}
19+
idx := func(val int, nums []int) int {
20+
for i, v := range nums {
21+
if v == val {
22+
return i
23+
}
24+
}
25+
return -1
26+
}(res.Val, inorder)
27+
if idx == -1 {
28+
return nil
29+
}
30+
res.Left = buildTree(preorder[1:idx+1], inorder[:idx])
31+
res.Right = buildTree(preorder[idx+1:], inorder[idx+1:])
32+
return res
33+
}

110. Balanced Binary Tree.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "math"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func isBalanced(root *TreeNode) bool {
12+
if root == nil {
13+
return true
14+
}
15+
16+
l := getHeight(root.Left)
17+
r := getHeight(root.Right)
18+
return l-r < 2 && r-l < 2 && isBalanced(root.Left) && isBalanced(root.Right)
19+
}
20+
21+
func getHeight(root *TreeNode) int {
22+
23+
if root == nil {
24+
return 0
25+
}
26+
27+
return int(math.Max(float64(getHeight(root.Left)), float64(getHeight(root.Right))) + 1)
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "math"
4+
5+
func maxProfit(prices []int) int {
6+
max_profit := 0
7+
min_price := math.MaxInt64
8+
for _, v := range prices {
9+
min_price = int(math.Min(float64(min_price), float64(v)))
10+
max_profit = int(math.Max(float64(max_profit), float64(v-min_price)))
11+
}
12+
return max_profit
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "math"
4+
5+
func maxProfit(prices []int) int {
6+
cur, max := 0, 0
7+
for i := 1; i < len(prices); i++ {
8+
cur = int(math.Max(float64(cur), float64(cur+prices[i]-prices[i-1])))
9+
max = int(math.Max(float64(cur), float64(max)))
10+
}
11+
return max
12+
}

139. Word Break.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
func wordBreak(s string, wordDict []string) bool {
4+
l := len(s)
5+
flags := make([]bool, l+1)
6+
flags[0] = true
7+
for i := 1; i <= l; i++ {
8+
for j := 0; j < i; j++ {
9+
if flags[j] == true && contain(s[j:i], wordDict) {
10+
flags[i] = true
11+
break
12+
}
13+
}
14+
}
15+
return flags[l]
16+
}
17+
18+
func contain(s string, wordDict []string) bool {
19+
for _, v := range wordDict {
20+
if s == v {
21+
return true
22+
}
23+
}
24+
return false
25+
}

2. Add Two Numbers.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
9+
result := &ListNode{0, nil}
10+
cursor := result
11+
leftBit, rightBit, carryBit := 0, 0, 0
12+
13+
for l1 != nil || l2 != nil || carryBit > 0 {
14+
if l1 != nil { //考虑5+5的情况
15+
leftBit = l1.Val
16+
l1 = l1.Next
17+
} else {
18+
leftBit = 0
19+
}
20+
21+
if l2 != nil {
22+
rightBit = l2.Val
23+
l2 = l2.Next
24+
} else {
25+
rightBit = 0
26+
}
27+
28+
cursor.Val = (leftBit + rightBit + carryBit) % 10
29+
carryBit = (leftBit + rightBit + carryBit) / 10
30+
31+
if l1 != nil || l2 != nil || carryBit > 0 {
32+
cursor.Next = &ListNode{0, nil}
33+
cursor = cursor.Next
34+
}
35+
}
36+
37+
return result
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func lengthOfLongestSubstring(s string) int {
4+
m := make(map[rune]int)
5+
start, max := -1, 0
6+
7+
for k, v := range s {
8+
if last, ok := m[v]; ok && last > start {
9+
start = last
10+
}
11+
m[v] = k
12+
if k-start > max {
13+
max = k - start
14+
}
15+
}
16+
return max
17+
}

0 commit comments

Comments
 (0)