2024每日刷题(140)
Leetcode—97. 交错字符串
2d动规实现代码
class Solution {
public:bool isInterleave(string s1, string s2, string s3) {int m = s1.length();int n = s2.length();int len = s3.length();if(m + n != len) {return false;}vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));dp[0][0] = true;for(int j = 1; j <= n; j++) {dp[0][j] = dp[0][j - 1] && s2[j - 1] == s3[j - 1];}for(int i = 1; i <= m; i++) {dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];}for(int i = 1; i <= m; i++) {for(int j = 1; j <= n; j++) {dp[i][j] = (dp[i][j - 1] && s2[j - 1] == s3[i + j - 1]) || (dp[i - 1][j] && s1[i - 1] == s3[i + j - 1]);}}return dp[m][n];}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!