FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc120.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
/
lc120.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (33 loc) · 1.08 KB
Breadcrumbs
leetcode
/
code
/
lc120.java
Copy path
File metadata and controls
34 lines (33 loc) · 1.08 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
package
code
;
import
java
.
util
.
List
;
/*
* 120. Triangle
* 题意:三角形的矩阵,求值最小的路径
* 难度:Medium
* 分类:Array, Dynamic Porgramming
* 思路:动态规划 dp[i] = min(dp[i-1],dp[i]) + val
* Tips:
*/
public
class
lc120
{
public
int
minimumTotal
(
List
<
List
<
Integer
>>
triangle
) {
int
len
=
triangle
.
get
(
triangle
.
size
()-
1
).
size
();
int
[]
dp
=
new
int
[
len
];
int
[]
dp2
=
new
int
[
len
];
int
index
=
0
;
int
res
=
Integer
.
MAX_VALUE
;
while
(
index
<
triangle
.
size
()){
List
<
Integer
>
ls
=
triangle
.
get
(
index
);
res
=
Integer
.
MAX_VALUE
;
for
(
int
i
=
0
;
i
<
ls
.
size
() ;
i
++) {
if
(
i
==
0
)
dp2
[
i
] =
dp
[
i
] +
ls
.
get
(
i
);
else
if
(
i
==
ls
.
size
()-
1
)
dp2
[
i
] =
dp
[
i
-
1
] +
ls
.
get
(
i
);
//注意是 i==ls.size()-1
else
dp2
[
i
] =
Math
.
min
(
dp
[
i
-
1
],
dp
[
i
]) +
ls
.
get
(
i
);
res
=
Math
.
min
(
dp2
[
i
],
res
);
}
dp
=
dp2
;
//两个一维数组
dp2
=
new
int
[
len
];
index
++;
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL