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

Commit32f0575

Browse files
author
Kyle Liu
authored
Merge pull request6boris#149 from kylesliu/develop
add 42 solution
2 parents8e3c739 +06a4fb8 commit32f0575

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
3+
import"fmt"
4+
5+
// 三次线性扫描
6+
functrap(height []int)int {
7+
iflen(height)==0 {
8+
return0
9+
}
10+
11+
n,ans:=len(height),0
12+
leftMax,rightMax:=make([]int,n),make([]int,n)
13+
14+
//求第i个矩形左边最高矩形的高度
15+
leftMax[0]=height[0]
16+
fori:=1;i<n;i++ {
17+
leftMax[i]=max(leftMax[i-1],height[i])
18+
}
19+
//求第i个矩形右边最高矩形的高度
20+
rightMax[n-1]=height[n-1]
21+
fori:=n-2;i>=0;i-- {
22+
rightMax[i]=max(rightMax[i+1],height[i])
23+
}
24+
25+
fori:=0;i<n;i++ {
26+
fmt.Println(i,leftMax[i],rightMax[i],height[i])
27+
ans+=min(leftMax[i],rightMax[i])-height[i]
28+
}
29+
30+
returnans
31+
}
32+
33+
funcmax(x,yint)int {
34+
ifx>y {
35+
returnx
36+
}
37+
returny
38+
}
39+
funcmin(x,yint)int {
40+
ifx>y {
41+
returny
42+
}
443
returnx
544
}

‎src/0042.Trapping-Rain-Water/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

89
funcTestSolution(t*testing.T) {
910
//测试用例
1011
cases:= []struct {
1112
namestring
12-
inputsbool
13-
expectbool
13+
inputs[]int
14+
expectint
1415
}{
15-
{"TestCacse 1",true,true},
16-
{"TestCacse 1",true,true},
17-
{"TestCacse 1",false,false},
16+
{"TestCase", []int{0,1,0,2,1,0,1,3,2,1,2,1},6},
1817
}
1918

2019
//开始测试
21-
for_,c:=rangecases {
22-
t.Run(c.name,func(t*testing.T) {
23-
ret:=Solution(c.inputs)
24-
if!reflect.DeepEqual(ret,c.expect) {
20+
fori,c:=rangecases {
21+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
22+
got:=trap(c.inputs)
23+
if!reflect.DeepEqual(got,c.expect) {
2524
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect,ret,c.inputs)
25+
c.expect,got,c.inputs)
2726
}
2827
})
2928
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp