[leetcode]_Interleaving String
下午去蹭了一发新浪的笔试。
炒鸡多的网络基础知识,总共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的更多相关文章
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode第一刷_Interleaving String
有关这样的字符串的题真是层出不穷啊,并且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比較好写的,用ispart[ ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [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 ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [LeetCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
随机推荐
- Selenium WebDriver 2.34.0 发布,支持Firefox22
Selenium WebDriver 2.34.0 发布,支持Firefox22http://automationqa.com/forum.php?mod=viewthread&tid=270 ...
- 单元测试JUnit 4
介绍 JUnit 4.x 是利用了 Java 5 的特性(Annotation)的优势,使得测试比起 3.x 版本更加的方便简单,JUnit 4.x 不是旧版本的简单升级,它是一个全新的框架,整个 ...
- Android平台Camera实时滤镜实现方法探讨(三)--通过Shader实现YUV转换RBG
http://blog.csdn.net/oshunz/article/details/50055057 文章例如该链接通过将YUV分成三个纹理,在shader中取出并且经过公式变换,转换成RGB.我 ...
- 基于SSM的单点登陆04
jdbc.properties JDBC_DRIVER=org.mariadb.jdbc.Driver JDBC_URL=jdbc:mariadb://127.0.0.1:3306/market JD ...
- html 5中的新特性之强化表单元素及属性
之前我们判断用户提交的是否是Email 的时候,往往使用js 进行判断,但在html5中可以有新的方式进行判断而且更简单 <!DOCTYPE html> <html lang=&qu ...
- 《React-Native系列》RN与native交互与数据传递
RN怎么与native交互的呢? 下面我们通过一个简单的Demo来实现:RN页面调起Native页面,Native页面选择电话本数据,将数据回传给RN展示. 首先是 Native侧 1.MainAct ...
- Linux系统中使用netcat命令的奇技淫巧
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- JAVA基础补漏--内部类
成员内部类 public class InnerClass { public static void main(String[] args) { System.out.println("++ ...
- 聊聊这两天在linux安装PHP7遇到的坑,真的是坑死人不偿命啊
前情摘要: 这两天要在虚拟机上部署项目,用于测试在linux上项目效果怎样,然后这两天就一直在部署apache+mysql+php 其实部署还是很简单的具体的apache和mysql部署方法请看其他两 ...
- BZOJ:(270,300]
9/30 BZOJ3038:线段树,不带lazy标记,直接修改叶子. BZOJ3211:同3038 BZOJ1406:将式子转换成[(x-1)*(x+1)%n==0]然后枚举i.当i=x-1时,i*( ...