一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

For example,

Given:

s1 = “aabcc”,

s2 = “dbbca”,

When s3 = “aadbbcbcac”, return true.

When s3 = “aadbbbaccc”, return false.

(二)解题

题目大意:给定三个字符串s1,s2,s3,判断s3是不是由s1和s2中的字符交叉存取组成,注意:在新字符串s3中应当保留原字符在s1和s2中的相对位置。

解题思想:采用动态规划的思想,每次将s1或s2中的字符与s3进行对比,分别有以下两种情况:

1、s1[i]==s3[k],此时i++,k++,继续进行比较

2、s2[j]==s3[k],此时j++,k++,然后继续比较

具体解释见代码注释:

class Solution {
public:
    bool isfind;//记录是否为Interleaving String
    bool isInterleave(string s1, string s2, string s3) {
        if (s3.size() != s1.size() + s2.size()) return false;//大小必须先满足条件
        isfind = false;//初始化isfind
        vector<vector<int>> isSearch;//用来记录那些已经查找过
        for(int i = 0 ; i < s1.length()+1 ;i++)//这里+1是为了防止find越界
        {
            vector<int> temp(s2.length()+1,0);
            isSearch.push_back(temp);
        }
        dfs(s1,s2,s3,0,0,0,isSearch);
        return isfind;
    }
    void dfs(string& s1, string& s2,string& s3,int i,int j,int k,vector<vector<int>>& isSearch)
    {
        if(i == s1.length()&& j==s2.length()&&k==s3.length()) {isfind=true;return;}
        if(find[i][j] == 1) return;
        find[i][j] = 1;
        if(isfind) return;//如果找到了就不需要递归了
        if(s1[i]==s3[k]&&i<s1.length()) dfs(s1,s2,s3,i+1,j,k+1,isSearch);//情况1
        if(s2[j]==s3[k]&&j<s2.length()) dfs(s1,s2,s3,i,j+1,k+1,isSearch);//情况2
    }
};

【一天一道LeetCode】#97. Interleaving String的更多相关文章

  1. [LeetCode] 97. Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...

  2. leetcode 97 Interleaving String ----- java

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  3. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  4. Leetcode#97 Interleaving String

    原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...

  5. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  6. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  7. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  8. 97. Interleaving String

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...

  9. leetcode@ [97] Interleaving Strings

    https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by th ...

随机推荐

  1. easyui datagrid 得到冻结列跟非冻结列

    var head = $('#ClassManagement').datagrid("options").columns; var fhead=$('#ClassManagemen ...

  2. java-反射深度剖析

    Java反射是Java语言一个很重要的特征,简单剖析下反射的定义.原理.使用.性能及应用场景. (一)定义 程序运行时,允许改动程序结构或变量类型,这种语言称为动态语言.java不属于动态语言,但提供 ...

  3. AleNet模型笔记

    谁创造了AlexNet? AlexNet是有Hinton大神的弟子Alex Krizhevsky提出的深度卷积神经网络.它可视为LeNet的更深更宽的版本. AlexNet主要用到的技术 成功使用Re ...

  4. Wcf host

    Uri baseAddress = new Uri(uri);//var binding = new WebHttpBinding(WebHttpSecurityMode.None); var bas ...

  5. 网络不能上网但能ping通处理

    同事电脑不能上网,经过检查可以排除网线和网络问题,默认网关以及网页地址都能ping通,从网上搜索说是LSP问题,但是根据其操作方式修复,还是无效. 最后,不知道怎么捣鼓的,问题解决了. 操作流程: 1 ...

  6. [HCNA]VLAN配置Access接口

    实验目的 1.理解VLAN的应用场景 2.掌握VLAN的基本配置 3.掌握Access接口的配置方法 4.掌握Access接口加入相应VLAN的方法 实验仪器 eNSP 实验原理 如网络拓扑图所示 各 ...

  7. Eclipse创建Maven工程

    Eclipse创建Maven工程: Eclipse: New  -> Other -> Maven Project -> Next -> webapp -> Finish ...

  8. redis的基本数据类型

    一:redis是一个开源的,使用C语言编写,支持网络,可基于内存亦可持久化的日志型,key-value方式存储的nosql数据库.作为缓存服务器,速度效率都很快,和memcache相似 redis支持 ...

  9. 深入浅出低功耗蓝牙(BLE)协议栈

    深入浅出低功耗蓝牙(BLE)协议栈 BLE协议栈为什么要分层?怎么理解蓝牙"连接"?如果蓝牙协议只有ATT没有GATT会发生什么? 协议栈框架 一般而言,我们把某个协议的实现代码称 ...

  10. grpc的服务注册与发现及负载

    参考文章: (1)https://segmentfault.com/a/1190000008672912 (2)https://grpc.io/docs/ (3)https://github.com/ ...