1.最长递增子序列
代码:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
//1.创建dp表
//2.初始化
//3.填表
//4.返回结果
int n = nums.size();
int ret = 1;
vector<int> dp(n, 1);
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(nums[j] < nums[i])
{
dp[i] = max(dp[j] + 1, dp[i]);
}
}
ret = max(ret, dp[i]);
}
return ret;
}
};
2.摆动序列
代码:
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
//1.创建dp表
//2.初始化
//3.填表
//4.返回结果
int n = nums.size();
int ret = 1;
vector<int> f(n, 1), g(n, 1);
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(nums[j] < nums[i])
f[i] = max(g[j] + 1, f[i]);
else if(nums[j] > nums[i])
g[i] = max(f[j] + 1, g[i]);
}
ret = max(ret, max(f[i], g[i]));
}
return ret;
}
};
3.最长递增子序列的个数
思路:
代码:
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> len(n, 1), count(n, 1);
int retlen = 1, retcount = 1;
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(nums[j] < nums[i])
{
if(len[j] + 1 == len[i])
count[i] += count[j];
else if(len[j] + 1 > len[i])//重新计数
len[i] = len[j] + 1, count[i] = count[j];
}
}
if(len[i] == retlen)
retcount += count[i];
else if(len[i] > retlen)
retlen = len[i], retcount = count[i];
}
return retcount;
}
};
4.最长数对链
代码:
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
//预处理,使大数对始终在小数对后面,满足子序列递增的条件
sort(pairs.begin(), pairs.end());
int n = pairs.size();
vector<int> dp(n, 1);
int ret = 1;
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(pairs[j][1] < pairs[i][0])
dp[i] = max(dp[i], dp[j] + 1);
}
ret = max(ret, dp[i]);
}
return ret;
}
};
5.最长定差子序列
代码:
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference) {
unordered_map<int, int> hash;//存放arr[i] - dp[i]进行绑定
hash[arr[0]] = 1;//初始化
int ret = 1;
for(int i = 1; i < arr.size(); i++)
{
hash[arr[i]] = hash[arr[i] - difference] + 1;
ret = max(ret, hash[arr[i]]);
}
return ret;
}
};
6.最长的斐波那契子序列的长度
代码:
class Solution {
public:
int lenLongestFibSubseq(vector<int>& arr) {
int n = arr.size();
unordered_map<int, int> hash;
for(int i = 0; i < n; i++) hash[arr[i]] = i;//把数组中的数值与下标进行绑定
int ret = 2;
vector<vector<int>> dp(n, vector<int>(n, 2));
for(int j = 2; j < n; j++)//固定最后一个位置
{
for(int i = 1; i < j; i++)//固定倒数第二个位置
{
int a = arr[j] - arr[i];
if(hash.count(a) && a < arr[i]) dp[i][j] = dp[hash[a]][i] + 1;
ret = max(ret, dp[i][j]);
}
}
return ret < 3 ? 0 : ret;
}
};
7.最长等差数列
代码:
class Solution {
public:
int longestArithSeqLength(vector<int>& nums) {
unordered_map<int, int> hash;
hash[nums[0]] = 0;
int n = nums.size();
vector<vector<int>> dp(n, vector<int>(n, 2));//创建dp表 + 初始化
int ret = 2;
for(int i = 1; i < n; i++)//固定倒数第二个数
{
for(int j = i + 1; j < n; j++)//枚举倒数第一个数
{
int a = 2 * nums[i] - nums[j];
if(hash.count(a))
dp[i][j] = dp[hash[a]][i] + 1;
ret = max(ret, dp[i][j]);
}
hash[nums[i]] = i;
}
return ret;
}
};
8.等差数列划分ii - 子序列
代码:
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
unordered_map<long long, vector<int>> hash;
for(int i = 0; i < nums.size(); i++) hash[nums[i]].push_back(i);
int sum = 0;
int n = nums.size();
vector<vector<int>> dp(n, vector<int>(n));
for(int j = 2; j < n; j++)//固定倒数第一个数
{
for(int i = 1; i < j; i++)//枚举倒数第二个数
{
long long a = (long long)2 * nums[i] - nums[j];
if(hash.count(a))
{
for(auto k : hash[a])
{
if(k < i) dp[i][j] += dp[k][i] + 1;//+1是因为k,i,j三个位置本身也可以作为等差数列,
//+=是因为k可以有很多个
}
}
sum += dp[i][j];
}
}
return sum;
}
};