[Leetcode] 378 Kth Smallest Element in a Sorted Matrix
来源:互联网 发布:中老年人学英语软件 编辑:程序博客网 时间:2024/05/01 14:28
题意: 在一个二维数组中按行有序,按列也有序,在这个二维数组中找第k大的数
思路: 使用最小堆的思想。在最小堆中找到第k个数
实现方法: 利用java中自带的优先队列实现最小堆的思想
具体代码实现:
public class Solution { public int kthSmallest(int[][] matrix, int k) { int result = 0; PriorityQueue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { if (o1 > o2) { return 1; } else { return -1; } } }); int row = matrix.length; int colum = matrix[0].length; for (int i = 0; i < row; i++) { for (int j = 0; j < colum; j++) { queue.add(matrix[i][j]); } } if (row * colum >= k) { for (int i = 1; i < k; i++) { queue.poll(); } } result = queue.peek(); return result; }}
0 0
- LeetCode[378] Kth Smallest Element in a Sorted Matrix
- LeetCode:378 Kth Smallest Element in a Sorted Matrix
- leetCode No.378 Kth Smallest Element in a Sorted Matrix
- leetcode(378):Kth Smallest Element in a Sorted Matrix
- [Leetcode] 378 Kth Smallest Element in a Sorted Matrix
- Leetcode 378 Kth Smallest Element in a Sorted Matrix
- 【Leetcode】Kth Smallest Element in a Sorted Matrix
- LeetCode—378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix
- leetcode之 Kth Smallest Element in a Sorted Matrix
- LeetCode:Kth Smallest Element in a Sorted Matrix
- leetcode---Kth Smallest Element in a Sorted Matrix
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
- Leetcode-378. Kth Smallest Element in a Sorted Matrix
- 378. [LeetCode]Kth Smallest Element in a Sorted Matrix
- 专题三-链表
- 详解在Visual Studio中使用git版本系统
- Android ViewTreeObserver简介
- Android打开微信本地数据库详细步骤二
- oracle和mysql数据类型的总结
- [Leetcode] 378 Kth Smallest Element in a Sorted Matrix
- BZOJ 3675 [Apio2014] 序列分割 斜率优化
- 第11课 Python中if分支结构
- 【游戏设计模式】之一 序言:架构,性能与游戏
- KCF 跟踪算法(3)
- 什么是FPS游戏
- 各个大神博客地址
- react native组件通信以及
- [Leetcode] 413. Arithmetic Slices