-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1143.最长公共子序列 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 1035.不相交的线 | ||
|
||
原来需要转化成‘最长公共子序列的长度’,一下子真不会 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxUncrossedLines(vector<int>& v, vector<int>& w) { | ||
// dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j] | ||
vector<vector<int>>dp(v.size()+1, vector<int>(w.size()+1, 0)); | ||
int result = 0; | ||
for (int i = 1; i <= v.size(); i++) { | ||
for (int j = 1; j <= w.size(); j++) { | ||
if (v[i - 1] == w[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[i][j] > result) result = dp[i][j]; | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 1143.最长公共子序列 | ||
|
||
抄了随想录 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int longestCommonSubsequence(string v, string w) { | ||
// dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j] | ||
vector<vector<int>>dp(v.size()+1, vector<int>(w.size()+1, 0)); | ||
int result = 0; | ||
for (int i = 1; i <= v.size(); i++) { | ||
for (int j = 1; j <= w.size(); j++) { | ||
if (v[i - 1] == w[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[i][j] > result) result = dp[i][j]; | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 53. 最大子序和 | ||
|
||
自己做的 喜喜 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxSubArray(vector<int>& v) { | ||
// dp[i] 以前i个数结尾的连续子数组最大和 | ||
int res = v[0]; | ||
vector<int>dp(v.size(),0); | ||
dp[0]=res; | ||
for(int i=1;i<v.size();i++){ | ||
dp[i]=max(v[i],dp[i-1]+v[i]); | ||
res=max(res,dp[i]); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |