对角线上的质数
难度:
标签:
题目描述
You are given a 0-indexed two-dimensional integer array nums
.
Return the largest prime number that lies on at least one of the diagonals of nums
. In case, no prime is present on any of the diagonals, return 0.
Note that:
- An integer is prime if it is greater than
1
and has no positive integer divisors other than1
and itself. - An integer
val
is on one of the diagonals ofnums
if there exists an integeri
for whichnums[i][i] = val
or ani
for whichnums[i][nums.length - i - 1] = val
.
In the above diagram, one diagonal is [1,5,9] and another diagonal is [3,5,7].
Example 1:
Input: nums = [[1,2,3],[5,6,7],[9,10,11]] Output: 11 Explanation: The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
Example 2:
Input: nums = [[1,2,3],[5,17,7],[9,11,10]] Output: 17 Explanation: The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
Constraints:
1 <= nums.length <= 300
nums.length == numsi.length
1 <= nums[i][j] <= 4*106
代码结果
运行时间: 32 ms, 内存: 27.4 MB
/*
题目思路:
1. 使用Java Stream遍历二维数组的对角线元素,包括主对角线和副对角线。
2. 过滤出质数并找到最大的质数。
*/
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Solution {
public int diagonalPrime(int[][] nums) {
int n = nums.length;
return Stream.concat(
IntStream.range(0, n).map(i -> nums[i][i]).boxed(),
IntStream.range(0, n).map(i -> nums[i][n - i - 1]).boxed()
)
.filter(this::isPrime)
.max(Integer::compare)
.orElse(0);
}
// 辅助方法:判断一个数是否为质数
private boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}
解释
方法:
该题解采用了直接遍历矩阵的两条对角线上的元素,并检查每个元素是否为质数的方法。遍历过程中,如果发现一个质数且比当前记录的最大质数大,则更新最大质数的记录。为了高效检查一个数是否为质数,题解中使用了一个辅助函数is_prime,该函数首先排除了一些特殊情况(如小于等于3的数和偶数),然后利用试除法检查是否有非1和自身的因子。
时间复杂度:
O(n * sqrt(n))
空间复杂度:
O(1)
代码细节讲解
🦆
如何确保此题解中的程序对稀异情况(如空的或超小的数组)的处理正确?
▷🦆
如果数组仅有一行或一列,程序将如何处理?这会影响结果的正确性吗?
▷🦆
在使用试除法检查质数时,为什么选择从3开始并每次增加2进行检查?
▷🦆
如果数组中有超过两行或两列,程序如何确保只检查两条对角线?
▷