Interleaving String leetcode

AC代码:

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        int i=0,m=s1.size(),n=s2.size();bool matrix[m+1][n+1];
        if((s1==""&&s2==s3)||(s2==""&&s1==s3))
          return true;
        if((s1==""&&s2!=s3)&&(s2==""&&s1!=s3))
          return false;
        if(s1.size()+s2.size()!=s3.size())
          return false;
        for(int i1=0;i1<m+1;i1++)
          for(int i2=0;i2<n+1;i2++){
            if(i2>0&&s3[i1+i2-1]==s2[i2-1]&&matrix[i1][i2-1])
              matrix[i1][i2]=true;
            else if(i1>0&&s3[i1+i2-1]==s1[i1-1]&&matrix[i1-1][i2])
              matrix[i1][i2]=true;
            else 
              matrix[i1][i2]=false;
            if(!i1&&!i2) matrix[i1][i2]=true;
          }
        return matrix[m][n];
    }
};


猜你喜欢

转载自blog.csdn.net/maryhaocoolcool/article/details/52043621