题目:

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. oracle 之 连接查询

    where 连接 select * from a,b //使用的是笛卡尔乘积  显示 a.count*b.count 条数 select * from a,b where a.id=b.id 其实只是 ...

  2. 51nod1057-N的阶乘(大数乘法巧解)

    这道大数乘法开始我是想套板子模拟的..然后就发现2/3的例子都wa了.(惊了).然后在思考后发现n2的板子的确过不了这么多的大数.(不看题的下场).所以,我在网上发现了分块求大数的方法.%%% 思路来 ...

  3. File类文件的常见操作

    boolean exists() 判断文件或者目录是否存在 boolean isFile()  判断是否是文件 boolean isDirectory() 判断是否是目录 String getPath ...

  4. RN 实现简易浏览器

    import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Dimensions, ...

  5. linux du

    命令参数: -a或-all  显示目录中个别文件的大小. -b或-bytes  显示目录或文件大小时,以byte为单位. -c或--total  除了显示个别目录或文件的大小外,同时也显示所有目录或文 ...

  6. Python 3种运行方式

    Python 命令行 >>>print('Hello World!') 小程序 在hello.py中写入如下,并保存: print('Hello World!') $python h ...

  7. 从DFS到记忆化DFS到动态规划

    什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求 ...

  8. jmeter线程组之间传参

    背景介绍: 使用jmeter做登录和搜索接口的测试: 登录接口请求头为:Content-Type: application/x-www-form-urlencoded; charset=UTF-8 搜 ...

  9. 内连接查询输出到datagridView

    实现步骤: 1. 新建两张对应表的类 例如: 第一张表对应的类 { class ManagerInfo { public Table1 group { get; set; } //重点 需要内连接的字 ...

  10. 把查询的数据导出到elsx表 关于流的概念

    1.获取到需要导出的数据   因为这个对象是PagedResulDto类型的   所以封装成Table的时候  传pageList.Items就可以了 PagedResultDto<Search ...