题目:
Given an integer array nums, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
代码:
#include <bits/stdc++.h>
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int len = nums.size();
int dp[len];
int ans = 1;
memset(dp, 0 , sizeof(dp));
dp[0] = 1; /*最短的 最长上升子序列 是本身*/
for(int i=1; i<len; i++){
bool flag = false;/* 用于判断第i个数之前是否有比nums[i]小的数 ,即是否上升*/
int x = 0;
for(int j=0; j<i; j++){
if(nums[j]<nums[i]){
flag = true;
x = max(x, dp[j]);
}
}
if(flag){
dp[i] = x + 1;
}else{
dp[i] = 1;
}
ans = max(ans,dp[i]);
}
return ans;
}
};