Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3c546df + 906f803 commit 05ff22aCopy full SHA for 05ff22a
problems/198.house-robber.md
@@ -102,7 +102,7 @@ return b;
102
103
## 代码
104
105
-- 语言支持:JS,C++,Python
+- 语言支持:JS,C++,Python,Java
106
107
JavaScript Code:
108
@@ -167,6 +167,29 @@ class Solution:
167
return cur
168
```
169
170
+Java Code:
171
+
172
+```java
173
+class Solution {
174
+ public int rob(int[] nums) {
175
+ if (nums == null || nums.length == 0) {
176
+ return 0;
177
+ }
178
+ int length = nums.length;
179
+ if (length == 1) {
180
+ return nums[0];
181
182
+ int prev = nums[0], cur = Math.max(nums[0], nums[1]);
183
+ for (int i = 2; i < length; i++) {
184
+ int temp = cur;
185
+ cur = Math.max(prev + nums[i], cur);
186
+ prev = temp;
187
188
+ return cur;
189
190
+}
191
+```
192
193
**复杂度分析**
194
195
- 时间复杂度:$O(N)$
0 commit comments