Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@ Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], thre
Output: 0
```

**Tags:** Math, String

## 题意
> 求2数之和

## 题解

### 思路1
> 。。。。

```go

```

### 思路2
> 思路2
```go

```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(mat [][]int, threshold int) int {
rows, cols := len(mat), len(mat[0])
cache := make([][]int, rows+1)
for i := 0; i <= rows; i++ {
cache[i] = make([]int, cols+1)
}

for i := rows - 1; i >= 0; i-- {
for j := cols - 1; j >= 0; j-- {
cache[i][j] = mat[i][j] + cache[i+1][j] + cache[i][j+1] - cache[i+1][j+1]
}
}

var ret, ml int
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
ml = min(rows-i, cols-j)
for k := 1; k <= ml; k++ {
sum := cache[i][j] - cache[i+k][j] - cache[i][j+k] + cache[i+k][j+k]
if sum <= threshold {
ret = max(ret, k)
}
}
}
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs [][]int
threshold int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 1, 2, 3, 4, 3, 2}, {1, 1, 2, 3, 4, 3, 2}, {1, 1, 2, 3, 4, 3, 2}}, 4, 2},
{"TestCase2", [][]int{{2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}}, 1, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.threshold)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.threshold)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading