有序矩阵中第k小的元素

给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。
请注意,它是排序后的第k小元素,而不是第k个元素。

示例:

matrix = [

[ 1, 5, 9],

[10, 11, 13],

[12, 13, 15]

],

k = 8,

返回 13。

说明:
你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n

根据二分搜索法,获取中间值,然后搜索他是否为第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小的元素的更多相关文章

  1. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  2. Java实现 LeetCode 378 有序矩阵中第K小的元素

    378. 有序矩阵中第K小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ ...

  3. leetcode.矩阵.378有序矩阵中第K小的元素-Java

    1. 具体题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1,  5, ...

  4. 378. 有序矩阵中第K小的元素

    Q: A: //O(NK) class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...

  5. [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 ...

  6. [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 ...

  7. 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)

    题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...

  8. 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [ ...

  9. 【力扣】有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素.请注意,它是排序后的第 k 小元素,而不是第 k 个不同的元素. 示例: matrix = [ [ 1, 5, ...

随机推荐

  1. bootstrap基本组件

    bootstrap分页   <nav>      <ul class="pagination">       <li><a href=&q ...

  2. leetcode287 Find the Duplicate Number

    思路: 转换成链表之后使用floyed判环法.转换之后重复的那个数字是唯一一个有多个前驱和一个后继的节点,因此是环的起始节点. 实现: class Solution { public: int fin ...

  3. ES-windos环境搭建(2)

    下载 打开elasticsearch官网.选择downloads. 选择elasticsearch,并单击Download. 这里我们不选择最新版,而是选择靠前的版本,下拉选择past release ...

  4. GetClassName 取得的类名有

    今天上午稍微跟踪了一下自己的项目里面的各个空间,得知GetClassName可以取到以下类名:Static\Edit\Button\ComboBox\msctls_trackbar32\SysTabC ...

  5. 【数据库-Azure SQL Database】SQL Server 如何将数据库备份到 Azure Storage

    打开本地的 SQL Server Management Studio.首先创建 Credentials.命令如下:   IF NOT EXISTS (SELECT * FROM sys.credent ...

  6. sysbench0.5安装介绍

    sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况,sysbench支持MySQL.PostgreSQL.Oracle数据库OLTP测试.它 ...

  7. Software Engineer(百赴美)

    http://talent.baidu.com/component1000/corp/baidu/html/BFM.html http://talent.baidu.com/baidu/web/tem ...

  8. Bootstrap 入门到精通

    介绍 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷.Bootstr ...

  9. codevs 3096 流输入练习——寻找Sb.VI

    时间限制: 1 s  空间限制: 16000 KB  题目等级 : 白银 Silver 题目描述 Description 已知某开放授权人员名叫Serb,由于经常修改各种数据,因此开发人员们都喊他SB ...

  10. lua_to_luac

    #!/bin/sh `rm -rf allLua.zip` `mkdir ./tempScripts` `mkdir ./tempScripts/scripts` `cp -a ./scripts/ ...