Skip to content

Commit 85e678a

Browse files
committed
三刷198
1 parent 6ec7f49 commit 85e678a

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

docs/0198-house-robber.adoc

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
[#0198-house-robber]
2-
= 198. House Robber
2+
= 198. 打家劫舍
33

4-
:stem: latexmath
4+
https://leetcode.cn/problems/house-robber/[LeetCode - 198. 打家劫舍 ^]
55

6-
{leetcode}/problems/house-robber/[LeetCode - House Robber^]
6+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,*如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警*
77

8-
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and *it will automatically contact the police if two adjacent houses were broken into on the same night*.
8+
给定一个代表每个房屋存放金额的非负整数数组,计算你 *不触动警报装置的情况下* ,一夜之内能够偷窃到的最高金额。
99

10-
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight *without alerting the police*.
10+
*示例 1:*
1111

12-
*Example 1:*
12+
....
13+
输入:[1,2,3,1]
14+
输出:4
15+
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
16+
偷窃到的最高金额 = 1 + 3 = 4 。
17+
....
1318

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [1,2,3,1]
17-
*Output:* 4
18-
*Explanation:* Rob house 1 (money = 1) and then rob house 3 (money = 3).
19-
Total amount you can rob = 1 + 3 = 4.
20-
----
19+
*示例 2:*
2120

22-
*Example 2:*
21+
....
22+
输入:[2,7,9,3,1]
23+
输出:12
24+
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
25+
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
26+
....
2327

24-
[subs="verbatim,quotes,macros"]
25-
----
26-
*Input:* [2,7,9,3,1]
27-
*Output:* 12
28-
*Explanation:* Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
29-
Total amount you can rob = 2 + 9 + 1 = 12.
28+
*提示:*
29+
30+
* `+1 <= nums.length <= 100+`
31+
* `+0 <= nums[i] <= 400+`
3032
31-
----
3233
3334
== 思路分析
3435

@@ -77,6 +78,15 @@ include::{sourcedir}/_0198_HouseRobber.java[tag=answer]
7778
include::{sourcedir}/_0198_HouseRobber_2.java[tag=answer]
7879
----
7980
--
81+
82+
三刷::
83+
+
84+
--
85+
[{java_src_attr}]
86+
----
87+
include::{sourcedir}/_0198_HouseRobber_3.java[tag=answer]
88+
----
89+
--
8090
====
8191

8292
== 参考资料

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ endif::[]
275275
|{doc_base_url}/0746-min-cost-climbing-stairs.adoc[题解]
276276
|✅ 动态规划
277277

278+
|{counter:codes2503}
279+
|{leetcode_base_url}/house-robber/[198. 打家劫舍^]
280+
|{doc_base_url}/0198-house-robber.adoc[题解]
281+
|✅ 动态规划
282+
278283

279284
|===
280285
˚
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0198_HouseRobber_3 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-16 20:19:02
9+
*/
10+
public int rob(int[] nums) {
11+
if (nums.length == 1) {
12+
return nums[0];
13+
}
14+
if (nums.length == 2) {
15+
return Math.max(nums[0], nums[1]);
16+
}
17+
nums[1] = Math.max(nums[0], nums[1]);
18+
for (int i = 2; i < nums.length; i++) {
19+
// dp[i] = max(nums[i] + dp[i-2], dp[i-1])
20+
nums[i] = Math.max(nums[i - 1], nums[i] + nums[i - 2]);
21+
}
22+
return nums[nums.length - 1];
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)