[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 ...
随机推荐
- 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130
求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...
- cdoj1337郭大侠与阴阳家
地址:http://acm.uestc.edu.cn/#/problem/show/1337 思路: 郭大侠与阴阳家 Time Limit: 3000/4000MS (Java/Others) ...
- springboot的Scheduled定时器不工作
问题情况 使用springboot,使用注解方式启动定时器进行业务调度. 在入口类中加了注解如下: package org.test.xyz; @SpringBootApplication @Enab ...
- 对Servlet请求或响应进行JMockit测试
对Servlet请求及响应进行mock方法, 通过getMockInstance方法对servlet进行打桩,对servlet提供的方法进行mock,替代真正的servlet请求或响应. 参考链接: ...
- 欧拉筛法(phi,d,prime)
筛法求素数的核心就是让每个合数被它的最小质因子筛掉,那么剩下来的就是素数了. 于是在这个过程中我们可以顺便求出每个数的φ().d().e(). ϕ:小于等于该数的与它互质的数的个数(一个数与其自身互质 ...
- Pandas基本功能
到目前为止,我们了解了三种Pandas数据结构以及如何创建它们.接下来将主要关注数据帧(DataFrame)对象,因为它在实时数据处理中非常重要,并且还讨论其他数据结构. 系列基本功能 编号 属性或方 ...
- Win7.还原默认打开方式
1.win7还原默认打开方式_百度知道.html(https://zhidao.baidu.com/question/1668708948433912307.html) Windows7:[47]打开 ...
- 关于Android中根据ID名动态获取资源的两个方法
在开发中, 我们习惯了类似下面这种方式去实现引用资源: context.getResources().getDrawable(R.drawable.flower); 但是,当我们提前知道这个资源的id ...
- LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- python学习笔记(unittest)
刚刚放假回来我想很多人都还没有缓过来吧 这次介绍一个python自带的测试框架 unitest #!/usr/bin/env python # -*- coding: utf_8 -*- import ...