标签:return 规划 tps leetcode 匹配 and string class com
https://www.cnblogs.com/grandyang/p/4298664.html
动态规划,dp[i][j]为true表示s1[i-1]可以与s3[i+j-1]匹配或s2[j-1]可以与s3[i+j-1]匹配
class Solution { public: bool isInterleave(string s1, string s2, string s3) { if(s1.size()+s2.size()!=s3.size()) return false; int len1=s1.size(),len2=s2.size(); vector<vector<bool>> dp(len1+1,vector<bool>(len2+1,false)); dp[0][0]=true; for(int i=1;i<=len1;++i) dp[i][0]=dp[i-1][0]&&s1[i-1]==s3[i-1]; for(int i=1;i<=len2;++i) dp[0][i]=dp[0][i-1]&&s2[i-1]==s3[i-1]; for(int i=1;i<=len1;++i) { for(int j=1;j<=len2;++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[len1][len2]; } };
leetcode 97 Interleaving String
标签:return 规划 tps leetcode 匹配 and string class com
原文地址:https://www.cnblogs.com/LiuQiujie/p/12794064.html