• 97. Interleaving String(js)


    97. Interleaving String

    Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

    Example 1:

    Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
    Output: true
    

    Example 2:

    Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
    Output: false
    题意:s3是否与s1和s2字符相交错相等
    代码如下:
    /**
     * @param {string} s1
     * @param {string} s2
     * @param {string} s3
     * @return {boolean}
     */
    var isInterleave = function(s1, s2, s3) {
        if(s3.length!==s1.length+s2.length){
            return false;
        }
        var res=[];
        for(var i=0;i<s1.length+1;i++){
            res[i]=new Array();
            for(var j=0;j<s2.length+1;j++){
                res[i][j]=false;
            }
        }
        
        for(var i=0;i<s1.length+1;i++){
            for(var j=0;j<s2.length+1;j++){
                if(i===0 && j===0){
                    res[i][j]=true;
                }else if(i===0){
                    res[i][j]=(res[i][j-1] && s2[j-1]===s3[i+j-1])
                }else if(j===0){
                    res[i][j]=(res[i-1][j] && s1[i-1]===s3[i+j-1])
                }else{
                    res[i][j]=(res[i-1][j] && s1[i-1]===s3[i+j-1]) || (res[i][j-1] && s2[j-1]===s3[i+j-1])
                }
            }
        }
        return res[s1.length][s2.length]
    };
  • 相关阅读:
    Leetcode86.分隔链表
    Leetcode39.组合总和
    Leetcode31.下一个排列
    剑指Offer35.复杂链表复制
    剑指Offer14-I.剪绳子
    剑指Offer38.字符串的排序
    Leetcode29.两数相除
    232. Implement Queue using Stacks
    程序员跳槽指南
    226. Invert Binary Tree
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/10707438.html
Copyright © 2020-2023  润新知