LeetCode 2614. Prime In Diagonal
来源:哔哩哔哩时间:2023-04-09 12:02:37

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 1and has no positive integer divisors other than 1and itself.

An integer valis on one of thediagonals of numsif there exists an integer ifor which nums[i][i] = valor an ifor which nums[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

作为一名数学系的学生,居然卡在了什么是素数,我又把1给忽略了,他不是素数。。。。

Runtime: 14 ms, faster than 20.00% of Java online submissions for Prime In Diagonal.

Memory Usage: 51.7 MB, less than 100.00% of Java online submissions for Prime In Diagonal.

关键词: