Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit b31bd30

Browse files
aQuaaQua
aQua
authored and
aQua
committed
378 adding
1 parent 0a3abfa commit b31bd30

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [378. Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/)
2+
3+
## 题目
4+
5+
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
6+
7+
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
8+
9+
```text
10+
Example:
11+
matrix = [
12+
[ 1, 5, 9],
13+
[10, 11, 13],
14+
[12, 13, 15]
15+
],
16+
k = 8,
17+
18+
return 13.
19+
```
20+
21+
Note:
22+
You may assume k is always valid, 1 ≤ k ≤ n2.
23+
24+
## 解题思路
25+
26+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Problem0378
2+
3+
func kthSmallest(mat [][]int, k int) int {
4+
if len(mat) == 0 || len(mat[0]) == 0 {
5+
return 0
6+
}
7+
8+
m, n := len(mat), len(mat[0])
9+
10+
return res
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Problem0378
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
var mat = [][]int{
11+
[]int{1, 5, 9},
12+
[]int{10, 11, 13},
13+
[]int{12, 13, 15},
14+
}
15+
16+
// tcs is testcase slice
17+
var tcs = []struct {
18+
k int
19+
ans int
20+
}{
21+
22+
{8, 13},
23+
24+
// 可以有多个 testcase
25+
}
26+
27+
func Test_kthSmallest(t *testing.T) {
28+
ast := assert.New(t)
29+
30+
for _, tc := range tcs {
31+
fmt.Printf("~~%v~~\n", tc)
32+
ast.Equal(tc.ans, kthSmallest(mat, tc.k), "输入:%v", tc)
33+
}
34+
}
35+
36+
func Benchmark_kthSmallest(b *testing.B) {
37+
for i := 0; i < b.N; i++ {
38+
for _, tc := range tcs {
39+
kthSmallest(tc.matrix, tc.k)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)