下午去蹭了一发新浪的笔试。

炒鸡多的网络基础知识,总共18道题,就写了8道左右吧,剩下的全是网络知识,这部分抽时间至少过一过。

其中一道算法题,回来跟嘟嘟商量,才发现是leetcode上的原题,连example都没有变,这可是道难度系数5的题,我嘞个去。

题目:给定三个字符串s1,s2,s3,判断s3是否能由s1和s2交错而成。

思路:

1、当s1当前字符 = s2当前字符 && s2当前字符 != s3当前字符 时,消s1.

2、同理状况 消s2.

3、难点在于当s1当前字符 = s2当前字符 = s3当前字符时,消谁,消错了可能引发后面的错误。我当时就想,那索性都尝试一遍,于是很直接的递归的想法就这么形成的。

    public boolean isInterleave(String s1, String s2, String s3) {

         int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
if(len1 + len2 != len3) return false;
else return isInterleaveCore(s1 , 0 , len1 - 1 , s2 , 0 , len2 - 1 , s3 , 0 , len3 - 1); }
public boolean isInterleaveCore(String s1 , int s1Start , int s1End ,
String s2 , int s2Start , int s2End ,
String s3 , int s3Start , int s3End){ int p1 = s1Start , p2 = s2Start , p3 = s3Start;
while(p3 <= s3End){
char ch3 = s3.charAt(p3);
if(p1 <= s1End && p2 <= s2End){
char ch1 = s1.charAt(p1);
char ch2 = s2.charAt(p2);
if(ch1 == ch3 && ch2 != ch3){
p1++;
p3++;
}else if(ch1 != ch3 && ch2 == ch3){
p2++;
p3++;
}else if(ch1 != ch3 && ch2 != ch3){
return false;
}else{
return isInterleaveCore(s1 , p1 + 1 , s1End , s2 , p2 , s2End , s3 , p3 + 1 , s3End) ||
isInterleaveCore(s1 , p1 , s1End , s2 , p2 + 1, s2End , s3 , p3 + 1 , s3End);
}
}else if(p1 <= s1End){
char ch1 = s1.charAt(p1);
if(ch1 != ch3) return false;
else{
p1++;
p3++;
}
}else if(p2 <= s2End){
char ch2 = s2.charAt(p2);
if(ch2 != ch3) return false;
else{
p2++;
p3++;
}
}
}
return true;
}

这里为自己点一个赞。第一次在考试中把递归给写出来了,证明之前的练习还是很成效的。非常( ^_^ )不错嘛。

但是这个算法过大集合时超时。其时间复杂度太高了。

网络上讲解还是要用DP。不会的心服口服。除了多练还是多练。

[leetcode]_Interleaving String的更多相关文章

  1. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. leetcode第一刷_Interleaving String

    有关这样的字符串的题真是层出不穷啊,并且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比較好写的,用ispart[ ...

  4. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  5. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  6. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  7. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

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

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

  9. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

随机推荐

  1. SqlHelper简单实现(通过Expression和反射)8.Sql Server数据处理类

    这个类基本上就是调用EntityHelper,ExpressionHelper和ObjectHelper来进行各种完整SQL的拼接操作. using System; using System.Conf ...

  2. JAVA中字符串问题

    JAVA中对象创建 通过new关键字来创建对象 通过newInstance()方法来创建对象 使用反序列化机制中IO流中恢复Java对象 通过clone()方法复制一个对象 直接量方式,String ...

  3. xx-net***简明使用教程

    简介 继psiphon3.lantern.shadowsocks后,翻 土啬 界就来个新角色:xx-net 这张图是.2016.8.30日Google最新的搜索结果,还是可以看出这款工具的火爆程度的, ...

  4. java:java静态代理与动态代理简单分析

    java静态代理与动态代理简单分析 转载自:http://www.cnblogs.com/V1haoge/p/5860749.html 1.动态代理(Dynamic Proxy) 代理分为静态代理和动 ...

  5. React-Navigation与Redux整合详解

    本文转自:文章地址:http://blog.csdn.net/u013718120/article/details/72357698 继react-navigation发布已经过去半年的时间,想必Re ...

  6. adb 不设别解决方案

    1.当然首先你得将手机里的usb debug选项选上,否则lsusb是不会有你的设备的 2. lsusb 查看usb设备id 3. sudo vim /etc/udev/rules.d/51-andr ...

  7. Spring_通过 FactoryBean 配置 Bean

    beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...

  8. 分布式链路监控与追踪系统Zipkin

    1.分布式链路监控与追踪产生背景2.SpringCloud Sleuth + Zipkin3.分布式服务追踪实现原理4.搭建Zipkin服务追踪系统5.搭建Zipkin集成RabbitMQ异步传输6. ...

  9. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  10. spark SQL学习(load和save操作)

    load操作:主要用于加载数据,创建出DataFrame save操作:主要用于将DataFrame中的数据保存到文件中 代码示例(默认为parquet数据源类型) package wujiadong ...