Skip to content

Commit

Permalink
完成"Pattern: Sliding Window"的introduction部分
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiwei-Feng committed Feb 19, 2021
1 parent 54bb3ef commit 7afc8aa
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# idea file
*.iml
.idea
3 changes: 3 additions & 0 deletions Pattern1 - Sliding Window/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module pattern1

go 1.13
15 changes: 15 additions & 0 deletions Pattern1 - Sliding Window/introduction/brute-force.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package introduction

func findAveragesOfSubArraysByBruteForce(K int, arr []int) []float64 {
results := make([]float64, len(arr)-K+1)
for i := 0; i <= len(arr)-K; i++ {
// 计算下一组K个元素的和
sum := 0
for j := i; j < i+K; j++ {
sum += arr[j]
}
// 计算平均
results[i] = float64(sum) / float64(K)
}
return results
}
23 changes: 23 additions & 0 deletions Pattern1 - Sliding Window/introduction/brute-fore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package introduction

import "testing"

func TestFindAveragesOfSubArraysByBruteForce(t *testing.T) {
arr := []int{1, 3, 2, 6, -1, 4, 1, 8, 2}
results := findAveragesOfSubArraysByBruteForce(5, arr)
expected := []float64{2.2, 2.8, 2.4, 3.6, 2.8}
if len(results) != len(expected) {
t.Errorf("Averages of subarrays of size K(5): %d, answer is %f, expect is %f", arr, results, expected)
} else {
eq := true
for i := 0; i < len(results); i++ {
if results[i] != expected[i] {
eq = false
break
}
}
if !eq {
t.Errorf("Averages of subarrays of size K(5): %d, answer is %f, expect is %f", arr, results, expected)
}
}
}
20 changes: 20 additions & 0 deletions Pattern1 - Sliding Window/introduction/sliding-window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package introduction

func findAveragesOfSubArraysBySlidingWindow(K int, arr []int) []float64 {
results := make([]float64, len(arr)-K+1)
windowSum := 0
windowStart := 0
for windowEnd := 0; windowEnd < len(arr); windowEnd++ {
// 把下个元素加上
windowSum += arr[windowEnd]
// 滑动窗口,特别地,如果没有达到K个则不滑动
if windowEnd >= K-1 {
// 当前windowSum计算了整个窗口的和
results[windowStart] = float64(windowSum) / float64(K)
// 当前窗口计算完后,移动窗口需要先减去原来窗口头部的元素
windowSum -= arr[windowStart]
windowStart++
}
}
return results
}
23 changes: 23 additions & 0 deletions Pattern1 - Sliding Window/introduction/sliding-window_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package introduction

import "testing"

func TestFindAveragesOfSubArraysBySlidingWindow(t *testing.T) {
arr := []int{1, 3, 2, 6, -1, 4, 1, 8, 2}
results := findAveragesOfSubArraysBySlidingWindow(5, arr)
expected := []float64{2.2, 2.8, 2.4, 3.6, 2.8}
if len(results) != len(expected) {
t.Errorf("Averages of subarrays of size K(5): %d, answer is %f, expect is %f", arr, results, expected)
} else {
eq := true
for i := 0; i < len(results); i++ {
if results[i] != expected[i] {
eq = false
break
}
}
if !eq {
t.Errorf("Averages of subarrays of size K(5): %d, answer is %f, expect is %f", arr, results, expected)
}
}
}

0 comments on commit 7afc8aa

Please sign in to comment.