Kth Smallest Element in a Sorted Matrix思路:1.首先这题把所有的数组遍历出来然后直接排序就可以AC..... 2.正常思路就是维系最大堆。
GitHub地址:https://github.com/corpsepiges/leetcode
目前java版本的答案大约进度是免费的差40题,python大约是一半,其他的等以后再补充。
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
public int compare(Integer a0, Integer a1) {
if (a0 > a1) {
return -1;
} else if (a0 < a1) {
return 1;
}
return 0;
}
});// 最大堆
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if ((i + 1) * (j + 1) > k) {
break;
}
maxHeap.offer(matrix[i][j]);
if (maxHeap.size() > k) {
maxHeap.poll();
}
}
}
return maxHeap.peek();
}
}