Leetcode 378 Kth Smallest Element in a Sorted Matrix
2017-06-21 13:17
417 查看
Leetcode 375 这个问题和375是同一个问题
可以用完全一样的方法来做
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.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Pairs> q = new PriorityQueue<>();
for(int j = 0; j < matrix[0].length; j++){
q.offer(new Pairs(matrix[0][j], 0, j));
}
int count = 0;
while(!q.isEmpty() && count++ < k - 1){
Pairs tmp = q.poll();
if(tmp.x == matrix.length - 1) continue;
q.offer(new Pairs(matrix[tmp.x + 1][tmp.y], tmp.x + 1, tmp.y));
}
return q.poll().val;
}
class Pairs implements Comparable<Pairs> {//这里就是用了一个class
int x, y, val;
public Pairs (int val, int x, int y) {
this.x = x;
this.y = y;
this.val = val;
}
@Override
public int compareTo (Pairs that) {
return this.val - that.val;
}
}
}
可以用完全一样的方法来做
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.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8, return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Pairs> q = new PriorityQueue<>();
for(int j = 0; j < matrix[0].length; j++){
q.offer(new Pairs(matrix[0][j], 0, j));
}
int count = 0;
while(!q.isEmpty() && count++ < k - 1){
Pairs tmp = q.poll();
if(tmp.x == matrix.length - 1) continue;
q.offer(new Pairs(matrix[tmp.x + 1][tmp.y], tmp.x + 1, tmp.y));
}
return q.poll().val;
}
class Pairs implements Comparable<Pairs> {//这里就是用了一个class
int x, y, val;
public Pairs (int val, int x, int y) {
this.x = x;
this.y = y;
this.val = val;
}
@Override
public int compareTo (Pairs that) {
return this.val - that.val;
}
}
}
相关文章推荐
- 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 378. Kth Smallest Element in a Sorted Matrix (Medium) (cpp)
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode[378] Kth Smallest Element in a Sorted Matrix
- leetcode378:Kth Smallest Element in a Sorted Matrix(medium)
- 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 :Binary Search:Kth Smallest Element in a Sorted Matrix(378)
- 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(medium)[堆求第K小的 ]
- LeetCode:378 Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix