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;
102102
103103## 代码
104104
105- - 语言支持:JS,C++,Python
105+ - 语言支持:JS,C++,Python,Java
106106
107107JavaScript Code:
108108
@@ -167,6 +167,29 @@ class Solution:
167167 return cur
168168```
169169
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+
170193** 复杂度分析**
171194
172195- 时间复杂度:$O(N)$
You can’t perform that action at this time.
0 commit comments