文章目录
-
- 1.最长回文子串
- 2.括号生成(回溯)
- 3.最大子序和
- 4.跳跃游戏
- 5.不同路径
- 6.最小路径和
- 7.买卖股票的最佳时机
1.最长回文子串
![动态规划专题_第1张图片](http://img.e-com-net.com/image/info8/eeb652c8965948ab8955dadd20f25237.jpg)
class Solution {
public:
string longestPalindrome(string s) {
int len = s.length();
if(len==0 || len==1) return s;
vector<vector<bool>> dp(len,vector<bool>(len,false));
for(int i=0;i<len;i++){
dp[i][i] = true;
}
int maxLen = 1;
int begin = 0;
for(int L=2;L<=len;L++){
for(int i=0;i<len;i++){
int j = i+L-1;
if(j>=len) break;
if(s[j]!=s[i]) dp[i][j]=false;
else{
if(j-i==1) dp[i][j]=true;
else dp[i][j] = dp[i+1][j-1];
}
if(dp[i][j] && L>maxLen){
maxLen = L;
begin = i;
}
}
}
return s.substr(begin,maxLen);
}
};
2.括号生成(回溯)
![动态规划专题_第2张图片](http://img.e-com-net.com/image/info8/537db47423cf409d9f9096cc93c8f665.jpg)
class Solution {
public:
void backtrack(vector<string>& res,string cur,int open,int close,int n){
if(cur.length()==n*2){
res.push_back(cur);
return;
}
if(open<n){
cur.push_back('(');
backtrack(res,cur,open+1,close,n);
cur.pop_back();
}
if(close<n && close<open){
cur.push_back(')');
backtrack(res,cur,open,close+1,n);
cur.pop_back();
}
}
vector<string> generateParenthesis(int n) {
vector<string> res;
backtrack(res,"",0,0,n);
return res;
}
};
3.最大子序和
![动态规划专题_第3张图片](http://img.e-com-net.com/image/info8/f234c8fb4f89480689866804b99cb191.jpg)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len,0);
dp[0] = nums[0];
int res = nums[0];
for(int i=1;i<len;i++){
dp[i] = max(dp[i-1],0)+nums[i];
res = max(res,dp[i]);
}
return res;
}
};
4.跳跃游戏
![动态规划专题_第4张图片](http://img.e-com-net.com/image/info8/2e4ea4d7edac414692574c6c0bb99608.jpg)
class Solution {
public:
bool canJump(vector<int>& nums) {
int len = nums.size();
int rightmost = 0;
for(int i=0;i<len;i++){
if(i<=rightmost){
rightmost = max(rightmost,i+nums[i]);
}
if(rightmost>=len-1) return true;
}
return false;
}
};
5.不同路径
![动态规划专题_第5张图片](http://img.e-com-net.com/image/info8/2cad18f30fd8456ea9f6936e5579f4ba.jpg)
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m,vector<int>(n,0));
for(int i=0;i<m;i++){
dp[i][0]=1;
}
for(int i=0;i<n;i++){
dp[0][i]=1;
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j] = dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
};
6.最小路径和
![动态规划专题_第6张图片](http://img.e-com-net.com/image/info8/c15d889501914bcd9590df692cf7b0b3.jpg)
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m=grid.size();
int n = grid[0].size();
vector<vector<int>> dp(m,vector<int>(n,0));
dp[0][0] = grid[0][0];
for(int i=1;i<m;i++){
dp[i][0] = dp[i-1][0]+grid[i][0];
}
for(int j=1;j<n;j++){
dp[0][j] = dp[0][j-1]+grid[0][j];
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j] = min(dp[i-1][j],dp[i][j-1])+grid[i][j];
}
}
return dp[m-1][n-1];
}
};
7.买卖股票的最佳时机
![动态规划专题_第7张图片](http://img.e-com-net.com/image/info8/d5dc76efecc54b078fbebfcd9d3c229c.jpg)
class Solution {
public:
int maxProfit(vector<int>& prices) {
int minPrice = INT_MAX;
int profit = 0;
for(int i=0;i<prices.size();i++){
if(prices[i]<minPrice){
minPrice = prices[i];
}else{
profit = max(profit,prices[i]-minPrice);
}
}
return profit;
}
};