File tree Expand file tree Collapse file tree 1 file changed +4
-7
lines changed Expand file tree Collapse file tree 1 file changed +4
-7
lines changed Original file line number Diff line number Diff line change @@ -130,18 +130,16 @@ public:
130
130
class Solution {
131
131
public int lengthOfLIS(int[] nums) {
132
132
int[] dp = new int[nums.length];
133
+ int res = 0;
133
134
Arrays.fill(dp, 1);
134
- for (int i = 0 ; i < dp.length; i++) {
135
+ for (int i = 1 ; i < dp.length; i++) {
135
136
for (int j = 0; j < i; j++) {
136
137
if (nums[i] > nums[j]) {
137
138
dp[i] = Math.max(dp[i], dp[j] + 1);
138
139
}
140
+ res = Math.max(res, dp[i]);
139
141
}
140
142
}
141
- int res = 0;
142
- for (int i = 0; i < dp.length; i++) {
143
- res = Math.max(res, dp[i]);
144
- }
145
143
return res;
146
144
}
147
145
}
@@ -294,7 +292,7 @@ function lengthOfLIS(nums: number[]): number {
294
292
295
293
``` rust
296
294
pub fn length_of_lis (nums : Vec <i32 >) -> i32 {
297
- let mut dp = vec! [1 ; nums . len () + 1 ];
295
+ let mut dp = vec! [1 ; nums . len ()];
298
296
let mut result = 1 ;
299
297
for i in 1 .. nums . len () {
300
298
for j in 0 .. i {
@@ -309,7 +307,6 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
309
307
```
310
308
311
309
312
-
313
310
<p align="center">
314
311
<a href="https://programmercarl.com/other/kstar.html " target="_ blank">
315
312
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
You can’t perform that action at this time.
0 commit comments