LeetCode#378 Kth Smallest Element in a Sorted Matrix
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.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
1 | matrix = [ |
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
解题思路
这是一道Medium题,但是却简单的有点不像话。题目要求是求一个每一行有序矩阵的第Kth数据,及求它第N大的数据。本来可能是需要使用数组存储每一行的当前指针balabala一大堆,但是其实只需要将所有值放到一个一维数组里边再对一维数组排序就可以直接取值了。
解题代码(.CPP)
1 | class Solution { |