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

Commit34590eb

Browse files
committed
add new
0 parents  commit34590eb

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+
functwoSum(nums []int,targetint) []int {
4+
m:=make(map[int]int)
5+
fori:=0;i<len(nums);i++ {
6+
c,ok:=m[nums[i]]
7+
ifok {
8+
r:= []int{c,i}
9+
returnr
10+
}else {
11+
m[target-nums[i]]=i
12+
}
13+
}
14+
r:= []int{0,0}
15+
returnr
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+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcisSymmetric(root*TreeNode)bool {
10+
returnisMirror(root,root)
11+
}
12+
13+
funcisMirror(t1*TreeNode,t2*TreeNode)bool {
14+
ift1==nil&&t2==nil {
15+
returntrue
16+
}
17+
ift1==nil||t2==nil {
18+
returnfalse
19+
}
20+
21+
returnt1.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+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
funcmaxDepth(root*TreeNode)int {
12+
ifroot==nil {
13+
return0
14+
}
15+
l:=maxDepth(root.Left)
16+
r:=maxDepth(root.Right)
17+
returnint(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+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funcbuildTree(preorder []int,inorder []int)*TreeNode {
10+
iflen(preorder)==0 {
11+
returnnil
12+
}
13+
res:=&TreeNode{
14+
Val:preorder[0],
15+
}
16+
iflen(preorder)==1 {
17+
returnres
18+
}
19+
idx:=func(valint,nums []int)int {
20+
fori,v:=rangenums {
21+
ifv==val {
22+
returni
23+
}
24+
}
25+
return-1
26+
}(res.Val,inorder)
27+
ifidx==-1 {
28+
returnnil
29+
}
30+
res.Left=buildTree(preorder[1:idx+1],inorder[:idx])
31+
res.Right=buildTree(preorder[idx+1:],inorder[idx+1:])
32+
returnres
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+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
funcisBalanced(root*TreeNode)bool {
12+
ifroot==nil {
13+
returntrue
14+
}
15+
16+
l:=getHeight(root.Left)
17+
r:=getHeight(root.Right)
18+
returnl-r<2&&r-l<2&&isBalanced(root.Left)&&isBalanced(root.Right)
19+
}
20+
21+
funcgetHeight(root*TreeNode)int {
22+
23+
ifroot==nil {
24+
return0
25+
}
26+
27+
returnint(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+
funcmaxProfit(prices []int)int {
6+
max_profit:=0
7+
min_price:=math.MaxInt64
8+
for_,v:=rangeprices {
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+
returnmax_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+
funcmaxProfit(prices []int)int {
6+
cur,max:=0,0
7+
fori:=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+
returnmax
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+
funcwordBreak(sstring,wordDict []string)bool {
4+
l:=len(s)
5+
flags:=make([]bool,l+1)
6+
flags[0]=true
7+
fori:=1;i<=l;i++ {
8+
forj:=0;j<i;j++ {
9+
ifflags[j]==true&&contain(s[j:i],wordDict) {
10+
flags[i]=true
11+
break
12+
}
13+
}
14+
}
15+
returnflags[l]
16+
}
17+
18+
funccontain(sstring,wordDict []string)bool {
19+
for_,v:=rangewordDict {
20+
ifs==v {
21+
returntrue
22+
}
23+
}
24+
returnfalse
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+
typeListNodestruct {
4+
Valint
5+
Next*ListNode
6+
}
7+
8+
funcaddTwoNumbers(l1*ListNode,l2*ListNode)*ListNode {
9+
result:=&ListNode{0,nil}
10+
cursor:=result
11+
leftBit,rightBit,carryBit:=0,0,0
12+
13+
forl1!=nil||l2!=nil||carryBit>0 {
14+
ifl1!=nil {//考虑5+5的情况
15+
leftBit=l1.Val
16+
l1=l1.Next
17+
}else {
18+
leftBit=0
19+
}
20+
21+
ifl2!=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+
ifl1!=nil||l2!=nil||carryBit>0 {
32+
cursor.Next=&ListNode{0,nil}
33+
cursor=cursor.Next
34+
}
35+
}
36+
37+
returnresult
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
funclengthOfLongestSubstring(sstring)int {
4+
m:=make(map[rune]int)
5+
start,max:=-1,0
6+
7+
fork,v:=ranges {
8+
iflast,ok:=m[v];ok&&last>start {
9+
start=last
10+
}
11+
m[v]=k
12+
ifk-start>max {
13+
max=k-start
14+
}
15+
}
16+
returnmax
17+
}

‎7. Reverse Integer.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import"math"
4+
5+
funcreverse(xint)int {
6+
7+
res:=0
8+
forx!=0 {
9+
carry:=x%10
10+
res=res*10+carry
11+
ifres>math.MaxInt32||res<math.MinInt32 {
12+
return0
13+
}
14+
x/=10
15+
}
16+
returnres
17+
}

‎9. Palindrome Number.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
funcisPalindrome(xint)bool {
4+
ifx<0 {
5+
returnfalse
6+
}
7+
ifx!=0&&x%10==0 {
8+
returnfalse
9+
}
10+
y:=0
11+
c:=x
12+
forx!=0 {
13+
y=y*10+x%10
14+
x/=10
15+
}
16+
returnc==y
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp