一天一道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. java线程与进程

    Java线程与进程 进程与线程的关系 进程里面至少有一个线程,进程间的切换会有较大的开销 线程必须依附在进程上,同一进程共享代码和数据空间 多线程的优势 多线程可以达到高效并充分利用cpu 线程使用的 ...

  2. hibernate4整合spring3出现java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

    解决办法 原先:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annota ...

  3. java-反射深度剖析

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

  4. 152. Maximum Product Subarray(中等, 神奇的 swap)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. css文本超出省略号

    终于完成了项目了,今天我就分享自己在项目中比较实用的一些功能的实现,第一个就是纯css文本超出省略号 /* 文本单行超出省略号 */ .textels { overflow: hidden; text ...

  6. 解释session

    我理解的session就是,多个页面都要使用某一个或一些数据,这时就可以用session,将数据暂时保存起来,这样其他的页面开启session,就能将那些数据拿出来使用.

  7. dict的操作和三级菜单

    dict的基本操作 # Author:nadech info = { "stu001":"sjp", "stu002":"cxx& ...

  8. Dapper连接与事务的简单封装

    增删改查方面,已经有Dapper.Extension这么强大的工具了,我也实在没啥好写的,就随手写了个看起来比较优雅的连接与事务的封装.在之后使用Dapper.Extension类库时,完全可以照搬进 ...

  9. Android简易实战教程--第四十九话《满屏拖动的控件》

    今天做个有意思的效果吧,控件的拖拽,简单实用,逻辑清晰点3分钟看完. 说的很高大上,其实就是拖动Button按钮跟着鼠标位置满手机屏幕跑罢了. 直接上简单的代码吧: public class Main ...

  10. Android开发技巧——定制仿微信图片裁剪控件

    拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...