-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
完成"Pattern: Sliding Window"的introduction部分
- Loading branch information
1 parent
54bb3ef
commit 7afc8aa
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# idea file | ||
*.iml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module pattern1 | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
Pattern1 - Sliding Window/introduction/sliding-window_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |