add js solution for findLengthOfLCIS

This commit is contained in:
Qi Jia
2021-07-13 19:23:18 -07:00
committed by GitHub
parent b64df0ff25
commit 2a247394a3

View File

@ -218,6 +218,49 @@ class Solution:
Go Go
Javascript
> 动态规划:
```javascript
const findLengthOfLCIS = (nums) => {
let dp = Array(nums.length).fill(1);
for(let i = 0; i < nums.length - 1; i++) {
if(nums[i+1] > nums[i]) {
dp[i+1] = dp[i]+ 1;
}
}
return Math.max(...dp);
};
```
> 贪心法:
```javascript
const findLengthOfLCIS = (nums) => {
if(nums.length === 1) {
return 1;
}
let maxLen = 1;
let curMax = 1;
let cur = nums[0];
for(let num of nums) {
if(num > cur) {
curMax += 1;
maxLen = Math.max(maxLen, curMax);
} else {
curMax = 1;
}
cur = num;
}
return maxLen;
};
```