Leetcode 378.有序矩阵中第k小的元素
有序矩阵中第k小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。
请注意,它是排序后的第k小元素,而不是第k个元素。
示例:
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
返回 13。
说明:
你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 。
根据二分搜索法,获取中间值,然后搜索他是否为第k个值。
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int len = matrix.length;
int low = matrix[0][0],high= matrix[len-1][len-1];
while(low<=high){
int mid = low + (high-low)/2;
int count = helper(matrix,mid);
if(count<k) low = mid+1;
else high = mid-1; //排除mid不在矩阵内的情况,所以只能等到low和high时才退出循环
}
return low;
}
private static int helper(int[][] matrix, int mid) {
int i = matrix.length-1,j=0;
int res = 0;
while(i>=0&&j<matrix[0].length){
if(matrix[i][j]>mid) i--;
else{
res+=i+1;
j++;
}
}
return res;
}
}
Leetcode 378.有序矩阵中第k小的元素的更多相关文章
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- Java实现 LeetCode 378 有序矩阵中第K小的元素
378. 有序矩阵中第K小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ ...
- leetcode.矩阵.378有序矩阵中第K小的元素-Java
1. 具体题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, ...
- 378. 有序矩阵中第K小的元素
Q: A: //O(NK) class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)
题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 9], [ ...
- 【力扣】有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素.请注意,它是排序后的第 k 小元素,而不是第 k 个不同的元素. 示例: matrix = [ [ 1, 5, ...
随机推荐
- .NET Core 1.0 CentOS7 尝试(三、使用VSCode创建一个Web应用)
参考地址:https://docs.asp.net/en/latest/tutorials/your-first-mac-aspnet.html 一.使用VSCode创建一个目录FirstWebApp ...
- XML文件的解析和序列化
序列化: private void createXml() { XmlSerializer serializer = Xml.newSerializer();// xml文件生成器 File file ...
- uvm_reg——寄存器模型(三)
uvm_reg 是uvm_reg_field , 包含所有uvm_reg_field 所有的函数.
- Eclipse下对MAVEN进行junit软件测试
一.Maven project management and build automation tool, more and more developers use it to manage the ...
- UIButton zoomin pressed
// Scale up on button press - (void) buttonPress:(UIButton*)button { button.transform = CGAffineTran ...
- (二)mybaits之ORM模型
前言:为什么还没有进入到mybatis的学习呢?因为mybatis框架的核心思想就是ORM模型,所以好好了解一下ORM模型是有必要哒. ORM模型 ORM(Object Relational Ma ...
- nuxt 初接触
对于nuxt服务端渲染让人动心的是不会再想vue一样去定义无数的路由了这一点是挺爽的!!! 先直接晒张图 在api这块增加了一个fetch方法 它会在组件每次加载前被调用(即在服务端或切换至目标路 ...
- 激活 IDEA, PyCharm
1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...
- IIS应用程序池"启用32位"导致服务不可用的503错误
原来运行正常的站点,突然不正常了,出现503错误.查看操作系统的日志查看器显示: 由于配置问题,无法加载模块 DLL“C:\Program Files (x86)\IIS\Asp.Net Core M ...
- PHP 头部utf-8
只是自己用的一些存储,请各位看官大大勿怪. header("Content-Type: text/html;charset=utf-8"); 2019年04月10日