mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0300.最长上升子序列.md):增加typescript版本
This commit is contained in:
@ -220,6 +220,27 @@ const lengthOfLIS = (nums) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript
|
||||
|
||||
```typescript
|
||||
function lengthOfLIS(nums: number[]): number {
|
||||
/**
|
||||
dp[i]: 前i个元素中,以nums[i]结尾,最长子序列的长度
|
||||
*/
|
||||
const dp: number[] = new Array(nums.length).fill(1);
|
||||
let resMax: number = 0;
|
||||
for (let i = 0, length = nums.length; i < length; i++) {
|
||||
for (let j = 0; j < i; j++) {
|
||||
if (nums[i] > nums[j]) {
|
||||
dp[i] = Math.max(dp[i], dp[j] + 1);
|
||||
}
|
||||
}
|
||||
resMax = Math.max(resMax, dp[i]);
|
||||
}
|
||||
return resMax;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user