Skip to content

Commit 18ad976

Browse files
committed
add new files
1 parent 65dc280 commit 18ad976

10 files changed

+266
-0
lines changed

100. Same Tree.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func isSameTree(p *TreeNode, q *TreeNode) bool {
10+
11+
if p == nil && q == nil {
12+
return true
13+
}
14+
if (p == nil && q != nil) || (p != nil && q == nil) {
15+
return false
16+
}
17+
if p.Val != q.Val {
18+
return false
19+
}
20+
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
21+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
//NB
10+
func flatten(root *TreeNode) {
11+
node := root
12+
for node != nil {
13+
if node.Left != nil {
14+
tmp := node.Left
15+
for tmp.Right != nil {
16+
tmp = tmp.Right
17+
}
18+
tmp.Right = node.Right
19+
node.Right = node.Left
20+
node.Left = nil
21+
}
22+
node = node.Right
23+
}
24+
}

125. Valid Palindrome.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
//TODO 速度不是很理想
10+
func isPalindrome1(s string) bool {
11+
pat := "[,:.@#--?\";!` ]"
12+
re, _ := regexp.Compile(pat)
13+
14+
s = re.ReplaceAllString(s, "")
15+
s = strings.ToLower(s)
16+
fmt.Println("s=", s)
17+
if s == "" {
18+
return true
19+
}
20+
j := len(s) - 1
21+
for i := 0; i < len(s)/2; i++ {
22+
if s[i] != s[j] {
23+
return false
24+
}
25+
j--
26+
}
27+
return true
28+
}

136. Single Number.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
//classic
4+
func singleNumber(nums []int) int {
5+
if len(nums) == 1 {
6+
return nums[0]
7+
}
8+
res := nums[0]
9+
for i := 1; i < len(nums); i++ {
10+
res ^= nums[i]
11+
}
12+
return res
13+
}

141. Linked List Cycle.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
//快慢指针,如果相遇则有环
9+
func hasCycle(head *ListNode) bool {
10+
if head == nil {
11+
return false
12+
}
13+
fast := head.Next
14+
slow := head
15+
16+
for slow != nil && fast != nil && fast.Next != nil {
17+
slow = slow.Next
18+
fast = fast.Next.Next
19+
if fast == slow {
20+
return true
21+
}
22+
}
23+
return false
24+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
//前中后基本类似
10+
func preorderTraversal(root *TreeNode) []int {
11+
res := []int{}
12+
if root == nil {
13+
return res
14+
}
15+
helper(&res, root)
16+
return res
17+
}
18+
19+
func helper(res *[]int, root *TreeNode) {
20+
*res = append(*res, root.Val)
21+
if root.Left != nil {
22+
helper(res, root.Left)
23+
}
24+
if root.Right != nil {
25+
helper(res, root.Right)
26+
}
27+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func postorderTraversal(root *TreeNode) []int {
10+
res := []int{}
11+
if root == nil {
12+
return res
13+
}
14+
helper(&res, root)
15+
return res
16+
}
17+
18+
func helper(res *[]int, root *TreeNode) {
19+
if root.Left != nil {
20+
helper(res, root.Left)
21+
}
22+
if root.Right != nil {
23+
helper(res, root.Right)
24+
}
25+
*res = append(*res, root.Val)
26+
}

151. Reverse Words in a String.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
//总觉得有更好的办法
9+
func reverseWords(s string) string {
10+
arr := strings.Split(s, " ")
11+
res := []string{}
12+
for i := 0; i < len(arr); i++ {
13+
if arr[i] != "" {
14+
res = append(res, arr[i])
15+
}
16+
}
17+
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
18+
res[i], res[j] = res[j], res[i]
19+
}
20+
21+
return strings.Replace(strings.Trim(fmt.Sprint(res), "[]"), " ", " ", -1)
22+
23+
}

155. Min Stack.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import "fmt"
4+
5+
//TODO 逻辑没问题,但是通过不了越界的测试
6+
type MinStack struct {
7+
cur int
8+
min []int
9+
data []int
10+
}
11+
12+
/** initialize your data structure here. */
13+
func Constructor() MinStack {
14+
min, data := []int{}, []int{}
15+
return MinStack{
16+
cur: -1,
17+
min: min,
18+
data: data,
19+
}
20+
}
21+
22+
func (this *MinStack) Push(x int) {
23+
this.cur++
24+
this.data = append(this.data, x)
25+
if this.cur > 0 {
26+
if x > this.min[this.cur-1] {
27+
this.min = append(this.min, this.min[this.cur-1])
28+
} else {
29+
this.min = append(this.min, x)
30+
}
31+
} else {
32+
this.min = append(this.min, x)
33+
}
34+
fmt.Println(this.min)
35+
}
36+
37+
func (this *MinStack) Pop() {
38+
this.cur--
39+
}
40+
41+
func (this *MinStack) Top() int {
42+
return this.data[this.cur]
43+
}
44+
45+
func (this *MinStack) GetMin() int {
46+
return this.min[this.cur]
47+
}
48+
49+
func main() {
50+
m := Constructor()
51+
m.Push(-2)
52+
m.Push(0)
53+
m.Push(-1)
54+
}

94. Binary Tree Inorder Traversal.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func inorderTraversal(root *TreeNode) []int {
10+
res := []int{}
11+
if root == nil {
12+
return res
13+
}
14+
helper(&res, root)
15+
return res
16+
}
17+
func helper(res *[]int, root *TreeNode) {
18+
if root.Left != nil {
19+
helper(res, root.Left)
20+
}
21+
*res = append(*res, root.Val)
22+
if root.Right != nil {
23+
helper(res, root.Right)
24+
}
25+
26+
}

0 commit comments

Comments
 (0)