Skip to content

Commit 274b92b

Browse files
committed
[E:56/531, M:54/969, H:6/386] add No: 100186 Zero Matrix LCCI
1 parent 49de4f6 commit 274b92b

File tree

9 files changed

+295
-1
lines changed

9 files changed

+295
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## [零矩阵](https://leetcode-cn.com/problems/zero-matrix-lcci/)
2+
3+
编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
4+
5+
 
6+
7+
**示例 1:**
8+
9+
`**输入:**
10+
[
11+
[1,1,1],
12+
[1,0,1],
13+
[1,1,1]
14+
]
15+
**输出:**
16+
[
17+
[1,0,1],
18+
[0,0,0],
19+
[1,0,1]
20+
]
21+
`
22+
23+
**示例 2:**
24+
25+
`**输入:**
26+
[
27+
[0,1,2,0],
28+
[3,4,5,2],
29+
[1,3,1,5]
30+
]
31+
**输出:**
32+
[
33+
[0,0,0,0],
34+
[0,4,5,0],
35+
[0,3,1,0]
36+
]
37+
`
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/100186/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input [][]int
18+
want [][]int
19+
}{
20+
{
21+
name: "test-[[1,1,1],[1,0,1],[1,1,1]]",
22+
input: [][]int{
23+
{1, 1, 1},
24+
{1, 0, 1},
25+
{1, 1, 1},
26+
},
27+
want: [][]int{
28+
{1, 0, 1},
29+
{0, 0, 0},
30+
{1, 0, 1},
31+
},
32+
},
33+
{
34+
name: "test-[[0,1,2,0],[3,4,5,2],[1,3,1,5]]",
35+
input: [][]int{
36+
{0, 1, 2, 0},
37+
{3, 4, 5, 2},
38+
{1, 3, 1, 5},
39+
},
40+
want: [][]int{
41+
{0, 0, 0, 0},
42+
{0, 4, 5, 0},
43+
{0, 3, 1, 0},
44+
},
45+
},
46+
{
47+
name: "test-[[0,1]]",
48+
input: [][]int{
49+
{0, 1},
50+
},
51+
want: [][]int{
52+
{0, 0},
53+
},
54+
},
55+
}
56+
57+
testLog := leet.NewTestLog(len(tests))
58+
defer testLog.Render()
59+
60+
timeoutDuration := time.Second * 2
61+
62+
for idx, test := range tests {
63+
// 超时检测
64+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
65+
solution.Export(test.input)
66+
cancel()
67+
})
68+
69+
if timeout {
70+
testLog.Fail(idx+1, test.name, "timeout")
71+
continue
72+
}
73+
74+
got := test.input
75+
if !reflect.DeepEqual(test.want, got) {
76+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
77+
continue
78+
}
79+
80+
testLog.Pass(idx+1, test.name)
81+
}
82+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(matrix [][]int) {
4+
setZeroes(matrix)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func setZeroes(matrix [][]int) {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(matrix [][]int) {
4+
setZeroes(matrix)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func setZeroes(matrix [][]int) {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## [零矩阵](https://leetcode-cn.com/problems/zero-matrix-lcci/)
2+
3+
编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
4+
5+
 
6+
7+
**示例 1:**
8+
9+
`**输入:**
10+
[
11+
[1,1,1],
12+
[1,0,1],
13+
[1,1,1]
14+
]
15+
**输出:**
16+
[
17+
[1,0,1],
18+
[0,0,0],
19+
[1,0,1]
20+
]
21+
`
22+
23+
**示例 2:**
24+
25+
`**输入:**
26+
[
27+
[0,1,2,0],
28+
[3,4,5,2],
29+
[1,3,1,5]
30+
]
31+
**输出:**
32+
[
33+
[0,0,0,0],
34+
[0,4,5,0],
35+
[0,3,1,0]
36+
]
37+
`
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/中等/100186/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input [][]int
18+
want [][]int
19+
}{
20+
{
21+
name: "test-[[1,1,1],[1,0,1],[1,1,1]]",
22+
input: [][]int{
23+
{1, 1, 1},
24+
{1, 0, 1},
25+
{1, 1, 1},
26+
},
27+
want: [][]int{
28+
{1, 0, 1},
29+
{0, 0, 0},
30+
{1, 0, 1},
31+
},
32+
},
33+
{
34+
name: "test-[[0,1,2,0],[3,4,5,2],[1,3,1,5]]",
35+
input: [][]int{
36+
{0, 1, 2, 0},
37+
{3, 4, 5, 2},
38+
{1, 3, 1, 5},
39+
},
40+
want: [][]int{
41+
{0, 0, 0, 0},
42+
{0, 4, 5, 0},
43+
{0, 3, 1, 0},
44+
},
45+
},
46+
{
47+
name: "test-[[0,1]]",
48+
input: [][]int{
49+
{0, 1},
50+
},
51+
want: [][]int{
52+
{0, 0},
53+
},
54+
},
55+
}
56+
57+
testLog := leet.NewTestLog(len(tests))
58+
defer testLog.Render()
59+
60+
timeoutDuration := time.Second * 2
61+
62+
for idx, test := range tests {
63+
// 超时检测
64+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
65+
solution.Export(test.input)
66+
cancel()
67+
})
68+
69+
if timeout {
70+
testLog.Fail(idx+1, test.name, "timeout")
71+
continue
72+
}
73+
74+
got := test.input
75+
if !reflect.DeepEqual(test.want, got) {
76+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
77+
continue
78+
}
79+
80+
testLog.Pass(idx+1, test.name)
81+
}
82+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(matrix [][]int) {
4+
setZeroes(matrix)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func setZeroes(matrix [][]int) {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(matrix [][]int) {
4+
setZeroes(matrix)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func setZeroes(matrix [][]int) {
13+
14+
}

0 commit comments

Comments
 (0)