mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #493 from jackeyjia/patch-13
add js solution for findLengthOfLCIS
This commit is contained in:
@ -218,6 +218,49 @@ class Solution:
|
||||
|
||||
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;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user