LeetCode 378. Kth Smallest Element in a Sorted Matrix 题解(C++)
来源:互联网 发布:java 堆栈pop报错 编辑:程序博客网 时间:2024/05/16 12:22
LeetCode 378. Kth Smallest Element in a Sorted Matrix 题解(C++)
题目描述
- 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.
举例
- matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
补充
- You may assume k is always valid, 1 ≤ k ≤ n^2.
思路
思路1
- 设置一个优先队列,先保存前k个元素,之后遍历矩阵,每次循环都将该元素放进队列,并从队列中出队一个元素(因为是优先队列,出队的元素是队列中的最大值),遍历完成返回队列头的元素即为所求的值。
思路2
- 使用优先队列先保存第一行的元素及每个元素对应的位置(这里优先队列的比较函数需要重新定义),之后执行k-1次循环,每次循环都从队列中出队一个最小的元素,并取该元素在矩阵中的下一行的元素入队,若该元素处于最后一行,则不需要取元素入队,最后返回队列头的元素。
代码
代码1
class Solution {public: int kthSmallest(vector<vector<int>>& matrix, int k) { priority_queue<int> queue; int i = 0, j = 0, t = 0; while (t < k) { queue.push(matrix[i][j]); ++j; if (j == matrix[0].size()) { ++i; j = 0; } ++t; } for (; i < matrix.size(); ++i) { for (; j < matrix[0].size(); ++j) { queue.push(matrix[i][j]); queue.pop(); } j = 0; } return queue.top(); }};
代码2
class Solution {public:struct comp{ bool operator()(const pair<int, pair<int, int>> &a, const pair<int, pair<int, int>> &b) { return a.first > b.first; }}; int kthSmallest(vector<vector<int>>& matrix, int k) { int row = matrix.size(); int column = matrix[0].size(); priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, comp> queue; for (int i = 0; i < column; ++i) { queue.push(make_pair(matrix[0][i], make_pair(0, i))); } while (--k) { int val = queue.top().first; int rowTemp = queue.top().second.first; int columnTemp = queue.top().second.second; queue.pop(); if (rowTemp != row - 1) { queue.push(make_pair(matrix[rowTemp + 1][columnTemp], make_pair(rowTemp + 1, columnTemp))); } } return queue.top().first; }};
0 0
- leetcode题解-378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix 题解(C++)
- LeetCode—378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
- Leetcode-378. Kth Smallest Element in a Sorted Matrix
- 378. [LeetCode]Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【LeetCode】 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- [LeetCode]378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode笔记:378. Kth Smallest Element in a Sorted Matrix
- C#/模拟键盘和鼠标的操作
- ES6 import/export:模块导入导出方式
- mysql innodb 行锁 insert
- 迷宫
- angularJs
- LeetCode 378. Kth Smallest Element in a Sorted Matrix 题解(C++)
- 根据前序遍历和中序遍历结果重建二叉树(递归方法)
- Django模块之include:
- 利用类反射和注解做自己的JUnit测试工具
- Extjs
- 安卓中的文件存储方式
- git
- C++中四种类型转换以及const_cast是否能改变常量的问题
- ios面试高频题(一)