赞
踩
思路如下:
1、查找第k小的元素,考虑使用优先队列PriorityQueue
2、需要注意,PriorityQueue没有poll(),添加元素要使用add()方法
class Solution { public int kthSmallest(int[][] matrix, int k) { int m=matrix.length; int n=matrix[0].length; PriorityQueue<Integer> queue=new PriorityQueue<>(); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ queue.add(matrix[i][j]); } } int res=-1; while(k>0){ res=queue.poll(); k--; } return res; } }
原题地址:
378. 有序矩阵中第K小的元素
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。