class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len, 1);
int res = 1; // 答案最少也有1
for(int i = 1; i < len; i++){
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
dp[i] = max(dp[j] + 1, dp[i]);
}
res = max(res, dp[i]); // 取长的子序列
}
}
return res;
}
};
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int res = 1; // 连续子序列最少是1
int count = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] > nums[i - 1]) count++; // 连续记录
else count = 1; // 不连续,从头开始
res = max(res, count); // 更新最长连续
}
return res;
}
};
if(nums[i] > nums[i - 1])??dp[i] = max(dp[i - 1] + 1, dp[i]);
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len, 1);
int res = 1;
for(int i = 1; i < len; i++){
if(nums[i] > nums[i - 1]){ // 连续记录
dp[i] = max(dp[i - 1] + 1, dp[i]);
}
res = max(dp[i], res);
}
return res;
}
};
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> dp (nums1.size() + 1, vector<int>(nums2.size() + 1, 0));
int result = 0;
for (int i = 1; i <= nums1.size(); i++) {
for (int j = 1; j <= nums2.size(); j++) {
if (nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
if (dp[i][j] > result) result = dp[i][j];
}
}
return result;
}
};
好耶,刚好刷完,找女朋友玩耍去咯~?