FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/src/PrimeInDiagonal.java at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
src
/
PrimeInDiagonal.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (34 loc) · 1.04 KB
Breadcrumbs
leetcode-algorithms
/
src
/
PrimeInDiagonal.java
Copy path
File metadata and controls
37 lines (34 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://leetcode.com/problems/prime-in-diagonal
// m: number of rows in matrix
// n: size of numbers in matrix
// T: O(m * sqrt(n))
// S: O(1)
public
class
PrimeInDiagonal
{
public
int
diagonalPrime
(
int
[][]
nums
) {
final
int
rows
=
nums
.
length
;
int
maxPrime
=
0
;
// left to right diagonal
for
(
int
row
=
0
;
row
<
rows
;
row
++) {
if
(
row
%
2
==
1
&&
row
==
rows
/
2
) {
continue
;
}
if
(
isPrime
(
nums
[
row
][
row
])) {
maxPrime
=
Math
.
max
(
maxPrime
,
nums
[
row
][
row
]);
}
}
// top right to bottom left diagonal
for
(
int
row
=
0
;
row
<
rows
;
row
++) {
if
(
isPrime
(
nums
[
row
][
rows
-
row
-
1
])) {
maxPrime
=
Math
.
max
(
maxPrime
,
nums
[
row
][
rows
-
row
-
1
]);
}
}
return
maxPrime
;
}
private
boolean
isPrime
(
int
x
) {
if
(
x
<=
1
)
return
false
;
for
(
int
i
=
2
;
i
*
i
<=
x
;
i
++) {
if
(
x
%
i
==
0
)
return
false
;
}
return
true
;
}
}
Back
|
FazBrowse Home
|
New Git URL