[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 ...
随机推荐
- Unity,android和IOS 防止八门神器注入
八门神器主要是不断筛选,来获取关键属性(比如金币)在内存中的地址,再根据该地址来修改指向的数据就可以成功. 因此,我们需要在金币读取和设置的时候,使用一个偏移量,来达到干扰的目的就可以了 未经仔细测试 ...
- redis 笔记04 服务器、复制
服务器 1. 一个命令请求从发送到完成主要包括以下步骤: 1). 客户端将命令请求发送给服务器 2). 服务器读取命令请求,并分析出命令参数 3). 命令执行器根据参数查找命令的实现函数,然后执行实现 ...
- Mybatis${}、#{}及使用#{}时指定jdbcType
一.Mybatis 的Mapper.xml语句中parameterType向SQL语句传参有两种方式:#{}和${} 我们经常使用的是#{},一般解说是因为这种方式可以防止SQL注入,简单的说#{}这 ...
- hydra简单使用示例
本内容为网上收集整理,仅作为备忘!! hydra简单使用示例: 破解https: # hydra -m /index.php -l muts -P pass.txt 10.36.16.18 https ...
- 《Maven实战》第14章 灵活的构建
面对不同环境的差异能够灵活的构建项目, 操作系统的差异 开发环境.测试环境.产品环境的差异(最常用) 不同客户的差异 Maven中灵活的构建:属性.资源过滤.profile 14.1Maven属性 6 ...
- centos7下apache+tomcat整合
前提 在系统中已经安装好了jdk.tomcat.apache #本人博客中jdk安装连接 http://www.cnblogs.com/xhkj/p/6545111.html #本人博客中tomcat ...
- 【bzoj3170】[Tjoi2013]松鼠聚会(数学题)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3170 这道题要在n个点中求一个点使其他点到该点的切比雪夫距离最小. 有个结论:$ (x ...
- select 下拉框 设置值
function setSelectOption(objSelect, targetValue){ if(objSelect){ var options = objSelect.options; if ...
- DataStage系列教程 (Pivot_Enterprise 行列转换)
有人提到Pivot_Enterprise这个组件,之前没有用过,今天捣腾了会,写下来供以后参考,如果有什么不对的,还请多指出,谢谢! Pivot_Enterprise主要用来进行行列转换. 1 示例 ...
- 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
第二次碰到这个问题了,记录一下 解决方案:1. 运行->cmd 2. cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 3. aspnet_ ...