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

Commitadd572e

Browse files
committed
add 643 solution
1 parentd375cf5 commitadd572e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcfindMaxAverage(nums []int,kint)float64 {
4+
tmp,ans:=0,-1
5+
fori:=0;i<len(nums);i++ {
6+
tmp+=nums[i]
7+
ifi>=k-1 {
8+
ans=max(ans,tmp)
9+
tmp-=nums[i-k+1]
10+
}
11+
}
12+
13+
returnfloat64(ans)/float64(k)
14+
}
15+
16+
funcmax(x,yint)int {
17+
ifx>y {
18+
returnx
19+
}
20+
returny
521
}

‎src/0643.Maximum-Average-Subarray-I/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
input1 []int
14+
input2int
15+
expectfloat64
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase", []int{1,12,-5,-6,50,3},4,12.75},
1918
}
2019

2120
//开始测试
2221
fori,c:=rangecases {
2322
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
23+
got:=findMaxAverage(c.input1,c.input2)
2524
if!reflect.DeepEqual(got,c.expect) {
2625
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
26+
c.expect,got,c.input1)
2827
}
2928
})
3029
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp