标签:oid height ace amp ret ble image com 记忆化
class Solution { public: bool result; void recursion(string& s1,int i1,string& s2,int i2,string& s3,int i3){ //printf("s1:%d %c\n",i1,s1[i1]); //递归终结条件 if(i3==s3.size()&&i1==s1.size()&&i2==s2.size()){ result=true;return;} //当前层处理逻辑 if(i1<s1.size()) if(s1[i1]==s3[i3]) recursion(s1,i1+1,s2,i2,s3,i3+1); //递归到下一层 if(i2<s2.size()) if(s2[i2]==s3[i3]) recursion(s1,i1,s2,i2+1,s3,i3+1); //清除当前层 return; } bool isInterleave(string s1, string s2, string s3) { result = false; recursion(s1,0,s2,0,s3,0); return result; } };
递推到最后的结果得到s3得起整个字符串能否被s1和s2的两个字符串交叉匹配
class Solution { public: bool isInterleave(string s1, string s2, string s3) { auto f = vector <int> (s2.size() + 1, false); int n = s1.size(), m = s2.size(), t = s3.size(); if (n + m != t) { return false; } f[0] = true; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { int p = i + j - 1; if (i > 0) { f[j] &= (s1[i - 1] == s3[p]); } if (j > 0) { f[j] |= (f[j - 1] && s2[j - 1] == s3[p]); } } } return f[m]; } };
public: bool result; bool** memo; void recursion(string& s1,int i1,string& s2,int i2,string& s3,int i3){ //printf("s1:%d %c\n",i1,s1[i1]); //递归终结条件 if(memo[i1][i2]==false)return; if(i3==s3.size()&&i1==s1.size()&&i2==s2.size()){ result=true;return;} //当前层处理逻辑 if(i1<s1.size()) if(s1[i1]==s3[i3]) recursion(s1,i1+1,s2,i2,s3,i3+1); //递归到下一层 if(i2<s2.size()) if(s2[i2]==s3[i3]) recursion(s1,i1,s2,i2+1,s3,i3+1); //清除当前层 memo[i1][i2]=false; return; } bool isInterleave(string s1, string s2, string s3) { result = false; memo = new bool*[s1.size()+1]; for(int i=0;i<s1.size()+1;i++){ memo[i] = new bool[s2.size()+1]; for(int j=0;j<s2.size()+1;j++){ memo[i][j]=true; }} recursion(s1,0,s2,0,s3,0); return result; } };
转自:C++声明二维数组
#include <iostream> #include <vector> using namespace std; int rows=2,columns=3;
使用一维数组模型二维数组
int a0[] = {1,2,3,4,5,6}; for(int i=0;i<rows;i++){ for(int j=0;j<columns;j++){ cout<<a0[i*columns+j]<<" ";//a0[i*columns+j]等价于a0[i][j] } cout<<endl; }
静态二维数组
int a1[2][3] = {1,2,3,4,5,6};
动态二维数组!要熟练
//申请空间 int** a2 = new int*[rows]; for(int i=0;i<rows;i++) a2[i] = new int[columns]; //释放空间 for(int i=0;i<rows;i++) delete []a2[i]; delete []a2;
利用vector创建二维数组
vector<vector<int> > a3(rows,vector<int>(columns)); for(int i=0;i<rows;i++){//初始化 for(int j=0;j<columns;j++){ a3[i][j] = a1[i][j]; } }
标签:oid height ace amp ret ble image com 记忆化
原文地址:https://www.cnblogs.com/BoysCryToo/p/13335212.html