[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 ...
随机推荐
- day1 python调用模块,密码加密
import getpass #加密密码 username = input("username:") password = getpass.getpass("passwo ...
- SQL中的几个判断是否存在
库是否存在if exists(select * from master..sysdatabases wherename=N'库名')print 'exists'elseprint 'not exist ...
- Django学习笔记之使用 Django项目开发框架
Django 项目是一个定制框架,它源自一个在线新闻 Web 站点,于 2005 年以开源的形式被释放出来.Django 框架的核心组件有: 用于创建模型的对象关系映射 为最终用户设计的完美管理界面 ...
- 20145229吴姗姗web安全基础实践
20145229吴姗姗web安全基础实践 基础与实践 基础问题 (1)SQL注入攻击原理,如何防御 SQL注入就是把SQL语句插入到之前已经定义好的语句中,作为网页中的比如用户名输入来达到攻击的目的, ...
- shell 中的 eval 及 crontab 命令
eval eval会对后面的命令进行两遍扫描,如果第一遍扫描后,命令是个普通命令,则执行此命令:如果命令中含有变量的间接引用,则保证间接引用的语义.也就是说,eval命令将会首先扫描命令行进行所有的置 ...
- Book Review of “The practice of programming” (Ⅲ)
The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...
- MySQL explain 、explain extended用法
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了,如: explai ...
- 多线程-栅栏CyclicBarrier
上一篇总结了闭锁CountDownLatch,这一篇总结一下栅栏CyclicBarrier.它们两者之间的区别主要是,闭锁是等待一个事件发生,比如上一篇的田径比赛,运动员等待裁判哨声一响就可以开始跑, ...
- [ SSH 两种验证方式原理 ]
SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户: (2) 用户会根据服务器发来的公钥对密码进行加密: (3) 加密后的信 ...
- sql内连接外连接自然连接
为什么我们要使用内连接和外连接呢?可以从两张或者多张表中找出,我们需要的属性. 这个比较好:http://www.cnblogs.com/youzhangjin/archive/2009/05/22/ ...