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

304. Range Sum Query 2D - Immutable #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from 304-Range-Sum-Query-2D-Immutable
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/0211_add_and_search_word/add_and_search_word_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "testing"
func TestAddAndSearchWord(t *testing.T) {
obj := Constructor()

for _, word := range []string{"bad", "dad", "mad"}{
for _, word := range []string{"bad", "dad", "mad"}{
obj.AddWord(word)
}

Expand Down
35 changes: 35 additions & 0 deletions src/304_Range_Sum_Query_2D/rsq.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
304. Range Sum Query 2D - Immutable
https://leetcode.com/problems/range-sum-query-2d-immutable/
*/
// 2019年02月27日

package rsq

// NumMatrix 累计区域和的数组
type NumMatrix struct {
dp [][]int
}

// Constructor 初始化构造函数
func Constructor(matrix [][]int) NumMatrix {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return NumMatrix{}
}
numMatrix := NumMatrix{dp: make([][]int, len(matrix)+1)}
for i := 0; i < len(numMatrix.dp); i++ {
numMatrix.dp[i] = make([]int, len(matrix[0])+1)
}

for i := 1; i <= len(matrix); i++ {
for j := 1; j <= len(matrix[0]); j++ {
numMatrix.dp[i][j] = numMatrix.dp[i-1][j] + numMatrix.dp[i][j-1] - numMatrix.dp[i-1][j-1] + matrix[i-1][j-1]
}
}
return numMatrix
}

// SumRegion 求区域和
func (nm *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
return nm.dp[row2+1][col2+1] - nm.dp[row1][col2+1] - nm.dp[row2+1][col1] + nm.dp[row1][col1]
}
35 changes: 35 additions & 0 deletions src/304_Range_Sum_Query_2D/rsq_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package rsq

import "testing"

func TestSunRegion(t *testing.T) {
matrix := [][]int{
{3, 0, 1, 4, 2},
{5, 6, 3, 2, 1},
{1, 2, 0, 1, 5},
{4, 1, 0, 1, 7},
{1, 0, 3, 0, 5},
}

obj := Constructor(matrix)

testData := [][]int{
{2, 1, 4, 3},
{1, 1, 2, 2},
{1, 2, 2, 4},
}
expected := []int{8, 11, 12}

for index, data := range testData {
if res := obj.SumRegion(data[0], data[1], data[2], data[3]); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}

if res := Constructor([][]int{}); res.dp != nil {
t.Errorf("expected nil, got %v", res.dp)
}
if res := Constructor(make([][]int, 3)); res.dp != nil {
t.Errorf("expected nil, got %v", res.dp)
}
}

AltStyle によって変換されたページ (->オリジナル) /