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

Commit6cf16d4

Browse files
committed
add new files
1 parent1a82fa9 commit6cf16d4

7 files changed

+179
-0
lines changed

‎509. Fibonacci Number.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
funcfib(Nint)int {
4+
ifN==0 {
5+
return0
6+
}
7+
ifN==1 {
8+
return1
9+
}
10+
returnfib(N-1)+fib(N-2)
11+
}

‎513. Find Bottom Left Tree Value.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
深度优先搜索整个树,记录层数
11+
每次搜索的时候,第一个碰到的元素,就是该层的最左子树
12+
*/
13+
varmaxd,valint
14+
15+
funcfindBottomLeftValue(root*TreeNode)int {
16+
maxd=0
17+
val=0
18+
dfs(root,1)
19+
returnval
20+
}
21+
22+
funcdfs(root*TreeNode,dint) {
23+
ifroot==nil {
24+
return
25+
}
26+
ifd>maxd {
27+
maxd=d
28+
val=root.Val
29+
}
30+
ifroot.Left!=nil {
31+
dfs(root.Left,d+1)
32+
}
33+
ifroot.Right!=nil {
34+
dfs(root.Right,d+1)
35+
}
36+
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import"fmt"
4+
5+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
/**
12+
前序遍历,先把每层的第一个节点加入结果数组,然后判断每个节点和第一个节点的大小
13+
*/
14+
varres []int
15+
16+
funclargestValues(root*TreeNode) []int {
17+
res= []int{}
18+
preorder(root,0)
19+
returnres
20+
}
21+
22+
funcpreorder(root*TreeNode,dint) {
23+
ifroot==nil {
24+
return
25+
}
26+
ifd==len(res) {
27+
res=append(res,root.Val)
28+
}else {
29+
res[d]=max(res[d],root.Val)
30+
}
31+
preorder(root.Left,d+1)
32+
preorder(root.Right,d+1)
33+
}
34+
funcmax(x,yint)int {
35+
ifx>y {
36+
returnx
37+
}
38+
returny
39+
}

‎520. Detect Capital.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import"strings"
4+
5+
/**
6+
按照规则转换之后,直接和原来的单词比较
7+
*/
8+
funcdetectCapitalUse(wordstring)bool {
9+
10+
l:=strings.ToLower(word)
11+
u:=strings.ToUpper(word)
12+
t:=strings.ToUpper(string(word[:1]))+strings.ToLower(word[1:])
13+
returnl==word||u==word||t==word
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
/**
4+
关键是要理解题目。。。
5+
*/
6+
funcfindLUSlength(astring,bstring)int {
7+
8+
ifa==b {
9+
return-1
10+
}
11+
iflen(a)>len(b) {
12+
returnlen(a)
13+
}
14+
returnlen(b)
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"math"
5+
)
6+
7+
/**
8+
考虑到有负数的情况,最大值有两种可能
9+
1.三个最大的正数
10+
2.两个最小的负数和一个最大的正数
11+
*/
12+
13+
funcmaximumProduct(nums []int)int {
14+
min1,min2:=math.MaxInt32,math.MaxInt32
15+
max1,max2,max3:=math.MinInt32,math.MinInt32,math.MinInt32
16+
for_,v:=rangenums {
17+
ifv<=min1 {
18+
min2,min1=min1,v
19+
}elseifv<=min2 {
20+
min2=v
21+
}
22+
ifv>=max1 {
23+
max3,max2,max1=max2,max1,v
24+
}elseifv>=max2 {
25+
max3,max2=max2,v
26+
}elseifv>=max3 {
27+
max3=v
28+
}
29+
}
30+
m1:=min1*min2*max1
31+
m2:=max1*max2*max3
32+
ifm1>m2 {
33+
returnm1
34+
}
35+
returnm2
36+
}

‎942. DI String Match.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
/**
4+
发现D的时候,就放最大的值进去,发现I的时候,就放最小的值进去
5+
*/
6+
7+
funcdiStringMatch(Sstring) []int {
8+
l:=len(S)
9+
ret:=make([]int,l+1)
10+
indexI,indexD:=0,len(S)
11+
fori:=0;i<=l;i++ {
12+
ifi==l {
13+
ret[i]=indexI
14+
break
15+
}
16+
ifS[i]=='D' {
17+
ret[i]=indexD
18+
indexD--
19+
}
20+
ifS[i]=='I' {
21+
ret[i]=indexI
22+
indexI++
23+
}
24+
}
25+
returnret
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp