1738:找出第 K 大的异或坐标值

This commit is contained in:
huangge1199 2021-05-19 09:15:46 +08:00
parent 3e38718a76
commit 267ea2a2eb

View File

@ -64,23 +64,26 @@ public class FindKthLargestXorCoordinateValue {
int xLength = matrix.length; int xLength = matrix.length;
int yLength = matrix[0].length; int yLength = matrix[0].length;
int[][] result = new int[xLength][yLength]; int[][] result = new int[xLength][yLength];
List<Integer> xor = new ArrayList<>(); PriorityQueue<Integer> xor=new PriorityQueue<>((o1,o2) -> {return o2-o1;});
for (int i = 0; i < xLength; i++) { result[0][0] = matrix[0][0];
for (int j = 0; j < yLength; j++) { xor.add(result[0][0]);
if (i == 0 && j == 0) { for (int i = 1; i < xLength; i++) {
result[0][0] = matrix[0][0]; result[i][0] = result[i - 1][0] ^ matrix[i][0];
} else if (i > 0 && j > 0) { xor.add(result[i][0]);
result[i][j] = result[i - 1][j] ^ result[i][j - 1] ^ result[i - 1][j - 1] ^ matrix[i][j]; }
} else if (i == 0) { for (int i = 1; i < yLength; i++) {
result[i][j] = result[i][j - 1] ^ matrix[i][j]; result[0][i] = result[0][i - 1] ^ matrix[0][i];
} else { xor.add(result[0][i]);
result[i][j] = result[i - 1][j] ^ matrix[i][j]; }
} for (int i = 1; i < xLength; i++) {
for (int j = 1; j < yLength; j++) {
result[i][j] = result[i - 1][j] ^ result[i][j - 1] ^ result[i - 1][j - 1] ^ matrix[i][j];
xor.add(result[i][j]); xor.add(result[i][j]);
} }
}for(int i=0;i<k-1;i++){
xor.poll();
} }
Collections.sort(xor); return xor.peek();
return xor.get(xLength * yLength - k);
} }
} }
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)