leetcode - Interleaving String
2013-04-21 10:32 张汉生 阅读(152) 评论(0) 编辑 收藏 举报题目描述:点击此处
1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int len1 = s1.length(); 7 int len2 = s2.length(); 8 int len3 = s3.length(); 9 if (len1+len2 != len3) 10 return false; 11 if (len1==0) 12 return s2==s3; 13 if (len2==0) 14 return s1==s3; 15 bool ** flag = new bool *[len1+1]; 16 int i, j; 17 for (i=0; i<=len1; i++){ 18 flag[i] = new bool[len2+1]; 19 for (j=0; j<=len2; j++) 20 flag[i][j] = false; 21 } 22 flag[0][0] = true; 23 for (i=0; i<=len1; i++){ 24 for (j=0; j<=len2; j++){ 25 if (i>0 && s1[i-1]==s3[i+j-1] && flag[i-1][j]) 26 flag[i][j] = true; 27 if (j>0 && s2[j-1]==s3[i+j-1] && flag[i][j-1]) 28 flag[i][j] = true; 29 } 30 } 31 bool rlt = flag[len1][len2]; 32 for (i=0; i<=len1; i++) 33 delete[] flag[i]; 34 delete []flag; 35 return rlt; 36 } 37 };