FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc221.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc221.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (35 loc) · 1.25 KB
Breadcrumbs
leetcode
/
code
/
lc221.java
Copy path
File metadata and controls
35 lines (35 loc) · 1.25 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
package
code
;
/*
* 221. Maximal Square
* 题意:0,1数组中最大正方形
* 难度:Medium
* 分类:Dynamic Programming
* 思路:三个正方形+上右下角位置,可以组成一个新的正方形
* Tips:和lc85作比较
*/
public
class
lc221
{
public
static
void
main
(
String
[]
args
) {
char
[][]
matrix
= {{
'1'
,
'0'
,
'1'
,
'0'
,
'0'
},{
'1'
,
'0'
,
'1'
,
'1'
,
'1'
},{
'1'
,
'1'
,
'1'
,
'1'
,
'1'
},{
'1'
,
'0'
,
'0'
,
'1'
,
'0'
}};
System
.
out
.
println
(
maximalSquare
(
matrix
));
}
public
static
int
maximalSquare
(
char
[][]
matrix
) {
if
(
matrix
.
length
==
0
)
return
0
;
int
[][]
dp
=
new
int
[
matrix
.
length
][
matrix
[
0
].
length
];
int
max
=
0
;
for
(
int
i
=
0
;
i
<
matrix
.
length
;
i
++) {
for
(
int
j
=
0
;
j
<
matrix
[
0
].
length
;
j
++) {
if
(
i
==
0
||
j
==
0
) {
dp
[
i
][
j
] =
matrix
[
i
][
j
]-
'0'
;
max
=
Math
.
max
(
dp
[
i
][
j
],
max
);
}
else
{
if
(
matrix
[
i
][
j
]==
'1'
) {
dp
[
i
][
j
] =
Math
.
min
(
Math
.
min
(
dp
[
i
-
1
][
j
],
dp
[
i
][
j
-
1
]),
dp
[
i
-
1
][
j
-
1
]) +
1
;
//dp[i][j] 最大正方形边长
max
=
Math
.
max
(
dp
[
i
][
j
],
max
);
}
}
}
}
return
max
*
max
;
}
}
Back
|
FazBrowse Home
|
New Git URL