题目:

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.

Example:

matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.

分析:采用优先队列,自定义比较规则,使得最小元素排在队首。

代码:

//优先级队列,是从jdk1.5开始提供的新的数据结构接口
//如果不提供Comparator的话,优先队列中元素默认按自然顺序排列
//每次从队列中取出的是具有最高优先权的元素。
import java.util.PriorityQueue; /**
* 已知一个n*m的矩阵,每一行、每一列都是有序的。
* 求这个矩阵中第k小的元素
* @author i333083
*
*/
public class KthSmallestEleInSortedMatrix { public static void main(String[] args) {
// TODO Auto-generated method stub int[][] a = new int[][]{{1,5,9,11},{2,6,10,14},{3,7,13,15}};
System.out.println(kthSmallest(a,11));
} //求矩阵中第k小的元素
public static int kthSmallest(int[][] matrix,int k){
int rows = matrix.length; //矩阵的行数
int cols = matrix[0].length;//列数 //优先队列
PriorityQueue<Tuple> queue = new PriorityQueue<Tuple>(); //入队列 /**
* 这里有一个设计非常巧妙的地方,将第一行升序的序列先入队列,根据Tuple类型的定义,最小的元素将会在队首。
* 每当队首元素出队列后,都将其所在列的下一个元素入队列,直到最后一行;
* 这样,就可以比较一个元素的同一行与同一列中相邻的元素中哪个较小,从而保证队首元素始终是矩阵中所有元素的最小值
*/
for(int j = 0; j < cols; ++j)
queue.add(new Tuple(0,j,matrix[0][j]));
//进行k-1次出队、入队,最后队首元素一定是所有元素中的第k小
for(int i = 0; i < k - 1; ++i){
Tuple p = queue.poll(); //出队列
System.out.print(p.val + " ");
if(p.x == rows - 1) //若是最后,则没有元素需要入队列了
continue;
queue.add(new Tuple(p.x + 1,p.y,matrix[p.x + 1][p.y]));
} return queue.poll().val;
}
} //实现Comparable接口要覆盖compareTo方法, 在compareTo方法里面实现比较
//自定义一个数据结构-元组,继承自comparable接口
class Tuple implements Comparable<Tuple>{
int x;
int y;
int val;
public Tuple(int x,int y,int val){
this.x = x;
this.y = y;
this.val = val;
} @Override
public int compareTo(Tuple t){
return this.val - t.val; //若新加入的元素较小,则排在队首
}
}

378. Kth Smallest Element in a Sorted Matrix(java,优先队列)的更多相关文章

  1. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

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

  3. 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 ...

  4. 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 ...

  5. 【Leetcode】378. Kth Smallest Element in a Sorted Matrix

    Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...

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

  7. 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)

    Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...

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

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

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

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

随机推荐

  1. extern 用法,全局变量与头文件(重复定义)

    转自 https://www.cnblogs.com/chengmin/archive/2011/09/26/2192008.html 当你要引用一个全局变量的时候,你就要声明,extern int  ...

  2. Custom Quality Profiles in SonarQube

    https://medium.com/ltunes/custom-quality-profiles-in-sonarqube-part-1-8754348b9369 Creating Custom Q ...

  3. C语言变量的作用域和存储类型

    1.动态局部变量:也称局部变量.自动变量,是指在函数内部定义的自动变量,不带static修饰,作用域是定义该变量的子程序.在退出函数后,变量自带内存会自动释放. 2.静态局部变量:是指在函数内部定义的 ...

  4. AtCoder Beginner Contest 117 解题报告

    果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include& ...

  5. LOJ#2444. 「NOI2011」阿狸的打字机

    题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有 \(28\) 个按键,分别印有 \(26\) 个小写英文字母和 B . P 两个字母. 经阿狸研究发现,这个打字机是 ...

  6. 第五个神奇的电梯(代码抢先看<1>)

    关于一些自认为比较独特的设计思路,也不知道是好还是坏,放在这里让大家一起看一下. 关于mian函数:因为采用了注册机制所以主函数比较简单. #include "stdafx.h" ...

  7. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  8. Echarts 修改字体样色 X、Y轴

    1.雷达图修改字体颜色 polar: [ { name:{ show: true, formatter: null, textStyle: { //设置颜色 color: '#109cad' } }, ...

  9. 点击返回上一页 wx.navigateTo不管用了

    做跳转的时候,发现想返回上一页,但是这个上一页又是tab上的页面,返回不了怎么办呢 wx.navigateTo({ url: '../search/search', })   解决方法: wx.reL ...

  10. mysql索引使用

    原文:http://www.jianshu.com/p/2b541c028157 索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型.在 ...