File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -102,7 +102,7 @@ return b;
102
102
103
103
## 代码
104
104
105
- - 语言支持:JS,C++,Python
105
+ - 语言支持:JS,C++,Python,Java
106
106
107
107
JavaScript Code:
108
108
@@ -167,6 +167,29 @@ class Solution:
167
167
return cur
168
168
```
169
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 first = nums[0 ], second = Math . max(nums[0 ], nums[1 ]);
183
+ for (int i = 2 ; i < length; i++ ) {
184
+ int temp = second;
185
+ second = Math . max(first + nums[i], second);
186
+ first = temp;
187
+ }
188
+ return second;
189
+ }
190
+ }
191
+ ```
192
+
170
193
** 复杂度分析**
171
194
172
195
- 时间复杂度:$O(N)$
You can’t perform that action at this time.
0 commit comments