题目:

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. java 之 基础加强(一)

    常用快捷键: alt + / 代码提示 ctrl + / 单行注释 取消注释 ctrl + shift +/ 多行注释(先选中内容) ctrl + shift +\ 取消注释(先选中内容) ctrl ...

  2. $on在构造器外部添加事件(通过$emit进行外部调用)$once执行一次的事件(通过$emit进行外部调用)$off关闭事件

    $on 在构造器外部添加事件. $on接收两个参数,第一个参数是调用时的事件名称,第二个参数是一个匿名方法. 如果按钮在作用域外部,可以利用$emit来执行. html <div id=&quo ...

  3. 浅谈FFT、NTT和MTT

    前言 \(\text{FFT}\)(快速傅里叶变换)是 \(O(n\log n)\) 解决多项式乘法的一个算法,\(\text{NTT}\)(快速数论变换)则是在模域下的,而 \(\text{MTT} ...

  4. 【ASP.NET】System.Web.Routing - HttpMethodConstraint Class

    你可以自己定义你的ASP.NET程序接收的get post put 或者delete请求. 使用这个约束的方式为: void Application_Start(object sender, Even ...

  5. js字符串与十六进制相互转换

    1.字符串(汉字)转换为十六进制 主要使用字符串.charCodeAt()方法,此方法返回一个字符的Unicode值,再用toString(16)方法,该方法是先将数字对象转换为二进制,再把二进制转化 ...

  6. jQuery页面加载初始化常用的三种方法

    当页面打开时我们需要执行一些操作,这个时候如果我们选择使用jquery的话,需要重写他的3中方法,自我感觉没什么区 别,看个人喜好了,第二种感觉比较简单明了: 第一种: 复制代码代码如下: <s ...

  7. mybatis(错误一) 项目启动时报“Result Maps collection already contains value forxxx”的解决方案

    Result Maps collection already contains value for xyx.dsw.dao.mapper.admin.quotationwish.TempTestTab ...

  8. vue运行报错--dependency

    ERROR Failed to compile with 1 errors 11:17:27 This dependency was not found: 解决方法:把报错所缺少的依赖都装上 如 xx ...

  9. 用html+css+js实现选项卡切换效果

    文章转载自:http://tongling.github.io/JSCards/ 用html+css+js实现选项卡切换效果 使用之前学过的综合知识,实现一个新闻门户网站上的常见选项卡效果: 文字素材 ...

  10. L1-044. 稳赢

    这种题不是考思维,就是考你细心程度还有基础知识的,代码如下: #include <iostream> #include <string> using namespace std ...