Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit18ad976

Browse files
committed
add new files
1 parent65dc280 commit18ad976

10 files changed

+266
-0
lines changed

‎100. Same Tree.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcisSameTree(p*TreeNode,q*TreeNode)bool {
10+
11+
ifp==nil&&q==nil {
12+
returntrue
13+
}
14+
if (p==nil&&q!=nil)|| (p!=nil&&q==nil) {
15+
returnfalse
16+
}
17+
ifp.Val!=q.Val {
18+
returnfalse
19+
}
20+
returnisSameTree(p.Left,q.Left)&&isSameTree(p.Right,q.Right)
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
//NB
10+
funcflatten(root*TreeNode) {
11+
node:=root
12+
fornode!=nil {
13+
ifnode.Left!=nil {
14+
tmp:=node.Left
15+
fortmp.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

Lines changed: 28 additions & 0 deletions
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+
funcisPalindrome1(sstring)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+
ifs=="" {
18+
returntrue
19+
}
20+
j:=len(s)-1
21+
fori:=0;i<len(s)/2;i++ {
22+
ifs[i]!=s[j] {
23+
returnfalse
24+
}
25+
j--
26+
}
27+
returntrue
28+
}

‎136. Single Number.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
//classic
4+
funcsingleNumber(nums []int)int {
5+
iflen(nums)==1 {
6+
returnnums[0]
7+
}
8+
res:=nums[0]
9+
fori:=1;i<len(nums);i++ {
10+
res^=nums[i]
11+
}
12+
returnres
13+
}

‎141. Linked List Cycle.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
typeListNodestruct {
4+
Valint
5+
Next*ListNode
6+
}
7+
8+
//快慢指针,如果相遇则有环
9+
funchasCycle(head*ListNode)bool {
10+
ifhead==nil {
11+
returnfalse
12+
}
13+
fast:=head.Next
14+
slow:=head
15+
16+
forslow!=nil&&fast!=nil&&fast.Next!=nil {
17+
slow=slow.Next
18+
fast=fast.Next.Next
19+
iffast==slow {
20+
returntrue
21+
}
22+
}
23+
returnfalse
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
//前中后基本类似
10+
funcpreorderTraversal(root*TreeNode) []int {
11+
res:= []int{}
12+
ifroot==nil {
13+
returnres
14+
}
15+
helper(&res,root)
16+
returnres
17+
}
18+
19+
funchelper(res*[]int,root*TreeNode) {
20+
*res=append(*res,root.Val)
21+
ifroot.Left!=nil {
22+
helper(res,root.Left)
23+
}
24+
ifroot.Right!=nil {
25+
helper(res,root.Right)
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcpostorderTraversal(root*TreeNode) []int {
10+
res:= []int{}
11+
ifroot==nil {
12+
returnres
13+
}
14+
helper(&res,root)
15+
returnres
16+
}
17+
18+
funchelper(res*[]int,root*TreeNode) {
19+
ifroot.Left!=nil {
20+
helper(res,root.Left)
21+
}
22+
ifroot.Right!=nil {
23+
helper(res,root.Right)
24+
}
25+
*res=append(*res,root.Val)
26+
}

‎151. Reverse Words in a String.go

Lines changed: 23 additions & 0 deletions
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+
funcreverseWords(sstring)string {
10+
arr:=strings.Split(s," ")
11+
res:= []string{}
12+
fori:=0;i<len(arr);i++ {
13+
ifarr[i]!="" {
14+
res=append(res,arr[i])
15+
}
16+
}
17+
fori,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+
returnstrings.Replace(strings.Trim(fmt.Sprint(res),"[]")," "," ",-1)
22+
23+
}

‎155. Min Stack.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import"fmt"
4+
5+
//TODO 逻辑没问题,但是通过不了越界的测试
6+
typeMinStackstruct {
7+
curint
8+
min []int
9+
data []int
10+
}
11+
12+
/** initialize your data structure here. */
13+
funcConstructor()MinStack {
14+
min,data:= []int{}, []int{}
15+
returnMinStack{
16+
cur:-1,
17+
min:min,
18+
data:data,
19+
}
20+
}
21+
22+
func (this*MinStack)Push(xint) {
23+
this.cur++
24+
this.data=append(this.data,x)
25+
ifthis.cur>0 {
26+
ifx>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+
returnthis.data[this.cur]
43+
}
44+
45+
func (this*MinStack)GetMin()int {
46+
returnthis.min[this.cur]
47+
}
48+
49+
funcmain() {
50+
m:=Constructor()
51+
m.Push(-2)
52+
m.Push(0)
53+
m.Push(-1)
54+
}

‎94. Binary Tree Inorder Traversal.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcinorderTraversal(root*TreeNode) []int {
10+
res:= []int{}
11+
ifroot==nil {
12+
returnres
13+
}
14+
helper(&res,root)
15+
returnres
16+
}
17+
funchelper(res*[]int,root*TreeNode) {
18+
ifroot.Left!=nil {
19+
helper(res,root.Left)
20+
}
21+
*res=append(*res,root.Val)
22+
ifroot.Right!=nil {
23+
helper(res,root.Right)
24+
}
25+
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp