class Solution {
public:
string LCS(string str1, string str2) {
int length = 0;
string res = "";
//遍历s1每个起始点
for(int i = 0; i < str1.length(); i++){
//遍历s2每个起点
for(int j = 0; j < str2.length(); j++){
int temp = 0;
string temps = "";
int x = i, y = j;
//比较每个起点为始的子串
while(x < str1.length() && y < str2.length() && str1[x] == str2[y]){
temps += str1[x];
x++;
y++;
temp++;
}
//更新更大的长度子串
if(length < temp){
length = temp;
res = temps;
}
}
}
return res;
}
};
超时,测试用例不能完全通过
时间复杂度:O(m^2*n)其中mmm是str1的长度,n是str2的长度,分别枚举两个字符串每个字符作为起点,后续检查子串长度最坏需要花费O(m)
空间复杂度:O(n),res属于返回必要空间,temps属于临时辅助空间,最坏情况下长度为n
class Solution {
public:
string LCS(string str1, string str2) {
//dp[i][j]表示到str1第i个个到str2第j个为止的公共子串长度
vector<vector<int> > dp(str1.length() + 1, vector<int>(str2.length() + 1, 0));
int max = 0;
int pos = 0;
for(int i = 1; i <= str1.length(); i++){
for(int j = 1; j <= str2.length(); j++){
//如果该两位相同
if(str1[i - 1] == str2[j - 1]){
//则增加长度
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else{
//该位置为0
dp[i][j] = 0;
}
//更新最大长度
if(dp[i][j] > max){
max = dp[i][j];
pos = i - 1;
}
}
}
return str1.substr(pos - max + 1, max);
}
};
时间复杂度:O(mn),其中m是str1的长度,n是str2的长度,遍历两个字符串所有字符
空间复杂度:O(mn),dp数组大小为m∗n
首先我们在左上角第一个格子的时候,有两种行走方式:如果向右走,相当于后面在一个(n−1)∗m的矩阵中查找从左上角到右下角的不同路径数;而如果向下走,相当于后面在一个n∗(m−1)的矩阵中查找从左上角到右下角不同的路径数。而(n−1)∗m的矩阵与n∗(m−1)的矩阵都是n∗m矩阵的子问题,因此可以使用递归。
class Solution {
public:
int uniquePaths(int m, int n) {
//矩阵只要有一条边为1,路径数就只有一种了
if(m == 1 || n == 1)
return 1;
//两个分支
return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
}
};
时间复杂度:O(m*n),其中m、n分别为矩阵的两边长,递归过程对于每个m最多都要经过每一种n
空间复杂度:O(m+n),递归栈的最大深度为矩阵两边从m、n都到了1

class Solution {
public:
int uniquePaths(int m, int n) {
//dp[i][j]表示大小为i*j的矩阵的路径数量
vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
//只有1行的时候,只有一种路径
if(i == 1){
dp[i][j] = 1;
continue;
}
//只有1列的时候,只有一种路径
if(j == 1){
dp[i][j] = 1;
continue;
}
//路径数等于左方格子的路径数加上上方格子的路径数
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m][n];
}
};
时间复杂度:O(mn),其中m、n分别为矩阵的两边长,两层遍历填充整个dp数组
空间复杂度:O(mn),辅助空间dp数组为二维数组
从矩阵左上角走到矩阵右下角,总共需要往下走m−11步,往右走n−1步,不同的走法路径在于往下和往右的组合情况,即在一堆往下和往右的序列中,每种排列的情况,序列一共有m+n−2个位置,选择其中n−1个位置作为往下,即不同的走法有
种情况
class Solution {
public:
int uniquePaths(int m, int n) {
//防止溢出
long res = 1;
for(int i = 1; i < n; i++)
//根据公式计算
res = res * (m + i - 1) / i;
return (int)res;
}
};
时间复杂度:O(n),计算过程需要从1遍历到n
空间复杂度:O(1),常数级变量,无额外辅助空间