FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc198.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
/
lc198.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (25 loc) · 714 Bytes
Breadcrumbs
leetcode
/
code
/
lc198.java
Copy path
File metadata and controls
25 lines (25 loc) · 714 Bytes
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
package
code
;
/*
* 198. House Robber
* 题意:数组最大和,不能选取相邻的两个数
* 难度:Easy
* 分类:Dynamic Programming
* 思路:经典的dp题,记一下
* Tips:时间复杂度为 O(n)
* lc213
*/
public
class
lc198
{
public
int
rob
(
int
[]
nums
) {
if
(
nums
.
length
==
0
)
return
0
;
if
(
nums
.
length
==
1
)
return
nums
[
0
];
int
[]
dp
=
new
int
[
nums
.
length
];
dp
[
0
] =
nums
[
0
];
dp
[
1
] =
Math
.
max
(
nums
[
0
],
nums
[
1
]);
for
(
int
i
=
2
;
i
<
nums
.
length
;
i
++) {
dp
[
i
] =
Math
.
max
((
dp
[
i
-
2
] +
nums
[
i
]),
dp
[
i
-
1
]);
//dp[i] 表示以 0~i 的数组的结果
}
return
dp
[
nums
.
length
-
1
];
}
}
Back
|
FazBrowse Home
|
New Git URL