Table of contents
3. Minimum cost to climb stairs
4. Convert the string to an integer
5. The longest common subsequence (2)
1. Fibonacci sequence
For this problem we use dynamic programming
First determine the state of the general case: the i-th Fibonacci number
Think about how to determine the state equation: his previous number + previous number
Initialization: F(0) = 1 ; F(1) = 1;
int Fibonacci(int n) {
int F[n + 1];
F[0] = 1;
F[1] = 1;
for (int i = 2; i < n; i++)
{
F[i] = F[i - 1] + F[i - 2];
}
return F[n - 1];
}
2. Jump the stairs
This is the shell of a Fibonacci application problem
The idea is almost the same as the previous question
int jumpFloor(int number) {
int dp[number];
dp[0]=1;
dp[1]=2;
for(int i=2;i<number;i++)
{
dp[i]=dp[i-1]+dp[i-2];
}
return dp[number-1];
}
3. Minimum cost to climb stairs
int minCostClimbingStairs(vector<int>& cost) {
// write code here
//状态:dp[i]:走到第i个台阶总共需要支付的费用
//状态方程:因为可以选择向上走一个,dp[i]=dp[i-1]+cost[i-1];
///或者两个台阶:dp[i]=dp[i-2]+cost[i-2];
//最小花费,就是两种情况的最小值
vector<int> dp(cost.size()+1);
dp[0]=0;dp[1]=0;
for(int i=2;i<=cost.size();i++)
{
dp[i]=min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
}
return dp[cost.size()];
}
4. Convert the string to an integer
The most classic code, almost no difficulty
int StrToInt(string str) {
int flag=1;
int i=0;
if(str[i]=='-' || str[0]=='+')
{
if(str[0]=='-')
flag=-1;
i++;
}
int ans=0;
for(;i<str.size();i++)
{
if(str[i]<'0'||str[i]>'9')
return 0;
ans=ans*10+str[i]-'0';
}
return flag*ans;
}
5. The longest common subsequence (2)
Not the longest substring (must be continuous)
This question is done with the most classic dynamic programming. First of all, we need to determine what the state is?
two strings s1, s2
State: s1 traverses to the subscript i, s2 traverses to the subscript j, the public subsequence at this time
How to determine the equation of state? Use a table to record the state moment, the length of the longest common subsequence
It is certain that when a subscript is 0, the length of the longest common subsequence is 0
So what about the rest?
Scenario 1: s1 traverses to subscript i, s2 traverses to subscript j, at this time, if the corresponding characters are equal , then the length of the longest common subsequence = subtracting a subscript at the same time (equivalent to the state after subtracting the equal characters Corresponding length) + 1 (this 1 is the longest common subsequence plus this equal character, corresponding to length + 1)
Scenario 2: s1 traverses to subscript i, and s2 traverses to subscript j. At this time, if the corresponding characters are not equal , then the sequence length at this time = max (s1 minus a subscript, the corresponding length of the s2 subscript remains unchanged and The s1 subscript remains unchanged, and the s2 subscript minus 1 corresponds to the length)
So far this table is completed
The following is the reverse deduction of what the longest common subsequence is based on this table (the length of the longest common subsequence)
Specify the direction to go in the opposite direction. If dp[x][y]==dp[x-1][y], go left (x--), if dp[x][y]==dp [x][y-1]
Then go up, if not, then directly add the s1/s2 corresponding to this subscript (the characters are equal anyway) to the string to be returned
So the final code
string LCS(string s1, string s2) {
// write code here
int n=s1.size();
int m=s2.size();
string ans;
int dp[n+1][m+1];
for(int j=0;j<=m;j++)
{
dp[0][j]=0;
}
for(int i=0;i<=n;i++)
{
dp[i][0]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
if(dp[n][m]==0) return "-1";
int x = n;
int y = m;
while (x != 0 && y!= 0) {
if (dp[x][y] == dp[x-1][y]) {
x--;
} else if (dp[x][y] == dp[x][y-1]) {
y--;
} else {
ans += s1[x-1];
x--;
y--;
}
}
reverse(ans.begin(), ans.end());
return ans;
}