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

This commit is contained in:
huangge1199 2021-05-19 09:02:36 +08:00
parent 382d30f13d
commit 41d01a2d00
3 changed files with 133 additions and 1 deletions

View File

@ -0,0 +1,88 @@
//给你一个二维矩阵 matrix 和一个整数 k 矩阵大小为 m x n 由非负整数组成
//
// 矩阵中坐标 (a, b) 可由对所有满足 0 <= i <= a < m 0 <= j <= b < n 的元素 matrix[i][j]
//标从 0 开始计数执行异或运算得到
//
// 请你找出 matrix 的所有坐标中第 k 大的值k 的值从 1 开始计数
//
//
//
// 示例 1
//
// 输入matrix = [[5,2],[1,6]], k = 1
//输出7
//解释坐标 (0,1) 的值是 5 XOR 2 = 7 为最大的值
//
// 示例 2
//
// 输入matrix = [[5,2],[1,6]], k = 2
//输出5
//解释坐标 (0,0) 的值是 5 = 5 为第 2 大的值
//
// 示例 3
//
// 输入matrix = [[5,2],[1,6]], k = 3
//输出4
//解释坐标 (1,0) 的值是 5 XOR 1 = 4 为第 3 大的值
//
// 示例 4
//
// 输入matrix = [[5,2],[1,6]], k = 4
//输出0
//解释坐标 (1,1) 的值是 5 XOR 2 XOR 1 XOR 6 = 0 为第 4 大的值
//
//
//
// 提示
//
//
// m == matrix.length
// n == matrix[i].length
// 1 <= m, n <= 1000
// 0 <= matrix[i][j] <= 106
// 1 <= k <= m * n
//
// Related Topics 数组
// 👍 23 👎 0
package leetcode.editor.cn;
import java.util.*;
//1738:找出第 K 大的异或坐标值
public class FindKthLargestXorCoordinateValue {
public static void main(String[] args) {
//测试代码
Solution solution = new FindKthLargestXorCoordinateValue().new Solution();
solution.kthLargestValue(new int[][]{{5, 2}, {1, 6}}, 1);
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int kthLargestValue(int[][] matrix, int k) {
int xLength = matrix.length;
int yLength = matrix[0].length;
int[][] result = new int[xLength][yLength];
List<Integer> xor = new ArrayList<>();
for (int i = 0; i < xLength; i++) {
for (int j = 0; j < yLength; j++) {
if (i == 0 && j == 0) {
result[0][0] = matrix[0][0];
} else if (i > 0 && j > 0) {
result[i][j] = result[i - 1][j] ^ result[i][j - 1] ^ result[i - 1][j - 1] ^ matrix[i][j];
} else if (i == 0) {
result[i][j] = result[i][j - 1] ^ matrix[i][j];
} else {
result[i][j] = result[i - 1][j] ^ matrix[i][j];
}
xor.add(result[i][j]);
}
}
Collections.sort(xor);
return xor.get(xLength * yLength - k);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,44 @@
<p>给你一个二维矩阵 <code>matrix</code> 和一个整数 <code>k</code> ,矩阵大小为 <code>m x n</code> 由非负整数组成。</p>
<p>矩阵中坐标 <code>(a, b)</code><strong></strong> 可由对所有满足 <code>0 &lt;= i &lt;= a &lt; m</code><code>0 &lt;= j &lt;= b &lt; n</code> 的元素 <code>matrix[i][j]</code><strong>下标从 0 开始计数</strong>)执行异或运算得到。</p>
<p>请你找出 <code>matrix</code> 的所有坐标中第 <code>k</code> 大的值(<strong><code>k</code> 的值从 1 开始计数</strong>)。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 1
<strong>输出:</strong>7
<strong>解释:</strong>坐标 (0,1) 的值是 5 XOR 2 = 7 ,为最大的值。</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 2
<strong>输出:</strong>5
<strong>解释:</strong>坐标 (0,0) 的值是 5 = 5 ,为第 2 大的值。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 3
<strong>输出:</strong>4
<strong>解释:</strong>坐标 (1,0) 的值是 5 XOR 1 = 4 ,为第 3 大的值。</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 4
<strong>输出:</strong>0
<strong>解释:</strong>坐标 (1,1) 的值是 5 XOR 2 XOR 1 XOR 6 = 0 ,为第 4 大的值。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == matrix.length</code></li>
<li><code>n == matrix[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
<li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= m * n</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 23</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long