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

Commit1a82fa9

Browse files
committed
add new files
1 parentf2e7d10 commit1a82fa9

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
因为给定的树是BST,所以可以知道如果采用后续遍历的方式,第一个找到的节点应该是最大的节点。所以采用递归的方式从最大的节点开始改变树的值
11+
*/
12+
varsumint
13+
14+
funcconvertBST(root*TreeNode)*TreeNode {
15+
sum=0
16+
helper(root)
17+
returnroot
18+
}
19+
20+
funchelper(root*TreeNode) {
21+
ifroot==nil {
22+
return
23+
}
24+
helper(root.Right)
25+
26+
root.Val+=sum
27+
sum=root.Val
28+
helper(root.Left)
29+
}

‎896. Monotonic Array.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
/**
4+
定义两个标志位,单调递增和单调递减
5+
扫描一遍,判断是否递增或者递减
6+
*/
7+
funcisMonotonic(A []int)bool {
8+
increasing,decreasing:=true,true
9+
fori:=1;i<len(A);i++ {
10+
ifA[i]>A[i-1] {
11+
decreasing=false
12+
}
13+
ifA[i]<A[i-1] {
14+
increasing=false
15+
}
16+
}
17+
returnincreasing||decreasing
18+
}

‎941. Valid Mountain Array.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
/**
4+
遍历一遍,先判断左边是不是符合规则
5+
再判断右边
6+
注意,如果有相等的元素,那一定是在两个部分的中间
7+
*/
8+
funcvalidMountainArray(A []int)bool {
9+
iflen(A)<3 {
10+
returnfalse
11+
}
12+
i:=0
13+
l:=len(A)
14+
fori+1<l&&A[i]<A[i+1] {
15+
i++
16+
}
17+
ifi==0||i==l-1 {
18+
returnfalse
19+
}
20+
fori+1<l&&A[i]>A[i+1] {
21+
i++
22+
}
23+
returni==l-1
24+
}
25+
26+
/**
27+
由于上面的速度太慢,于是找到了最快的AC如下:
28+
先找到最大的(就是中间的元素)
29+
30+
*/
31+
funcvalidMountainArray1(a []int)bool {
32+
iflen(a)<3 {
33+
returnfalse
34+
}
35+
max:=0
36+
index:=0
37+
fori,v:=rangea {
38+
ifv>max {
39+
max=v
40+
index=i
41+
}
42+
}
43+
ifindex==0||index==len(a)-1 {
44+
returnfalse
45+
}
46+
fori,j:=0,1;i<=index&&j<=index;i,j=i+1,j+1 {
47+
ifa[i]>=a[j] {
48+
returnfalse
49+
}
50+
}
51+
52+
fori,j:=index,index+1;i<len(a)&&j<len(a);i,j=i+1,j+1 {
53+
ifa[i]<=a[j] {
54+
returnfalse
55+
}
56+
}
57+
returntrue
58+
59+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
/**
4+
先计算数组中所有偶数的和
5+
遍历操作
6+
如果之前是偶数,从和中减去
7+
执行加操作
8+
如果之后是偶数,再加到和里
9+
*/
10+
funcsumEvenAfterQueries(A []int,queries [][]int) []int {
11+
12+
sum:=0
13+
for_,v:=rangeA {
14+
ifv%2==0 {
15+
sum+=v
16+
}
17+
}
18+
ret:= []int{}
19+
for_,v:=rangequeries {
20+
ifA[v[0]]%2==0 {
21+
sum-=A[v[0]]
22+
}
23+
A[v[0]]+=v[1]
24+
ifA[v[0]]%2==0 {
25+
sum+=A[v[0]]
26+
}
27+
ret=append(ret,sum)
28+
}
29+
returnret
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp