378. Kth Smallest Element in a Sorted Matrix
来源:互联网 发布:数据库不能附加 编辑:程序博客网 时间:2024/06/05 12:41
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:
matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15]],k = 8,return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
//使用vector保存matrix中的元素,对vector排序,结果返回vector中的第k-1个元素。class Solution {public:int kthSmallest(vector<vector<int>>& matrix, int k) {int row, column;vector<int> vec;row = matrix.size();column = matrix[0].size();vec.reserve(row*column);for (vector<vector<int>>::iterator i = matrix.begin(); i != matrix.end(); ++i){for (vector<int>::iterator j = i->begin(); j != i->end(); ++j)vec.push_back(*j);}sort(vec.begin(),vec.end());return vec.at(k-1);}};
阅读全文
0 0
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- 378. Kth Smallest Element in a Sorted Matrix
- 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
- leecode 378. Kth Smallest Element in a Sorted Matrix java
- 378. Kth Smallest Element in a Sorted Matrix
- 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
- 网络编程学习笔记(三)TCP协议及客户端与服务端交互Demo
- 各种图表绘制
- 索引组织表+创建索引组织表
- vb.net 教程 3-4 窗体编程 公共控件9 ListView 2
- Pull解析XML时出现System.err: org.xmlpull.v1.XmlPullParserException: Expected a quoted string (position:DO
- 378. Kth Smallest Element in a Sorted Matrix
- 闭包
- C语言中数组名的理解
- Redis的快照功能
- Python25行代码人脸识别
- 类加载器
- padas 生成excel 增加sheet表
- fuzhuo-----Problem 1019 猫捉老鼠
- 03-JDBC学习手册:JDBC中几个重要接口和异常处理