[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当前字符时,消谁,消错了可能引发后面的错误。我当时就想,那索性都尝试一遍,于是很直接的递归的想法就这么形成的。


 1    public boolean isInterleave(String s1, String s2, String s3) {
2
3 int len1 = s1.length();
4 int len2 = s2.length();
5 int len3 = s3.length();
6 if(len1 + len2 != len3) return false;
7 else return isInterleaveCore(s1 , 0 , len1 - 1 , s2 , 0 , len2 - 1 , s3 , 0 , len3 - 1);
8
9 }
10 public boolean isInterleaveCore(String s1 , int s1Start , int s1End ,
11 String s2 , int s2Start , int s2End ,
12 String s3 , int s3Start , int s3End){
13
14 int p1 = s1Start , p2 = s2Start , p3 = s3Start;
15 while(p3 <= s3End){
16 char ch3 = s3.charAt(p3);
17 if(p1 <= s1End && p2 <= s2End){
18 char ch1 = s1.charAt(p1);
19 char ch2 = s2.charAt(p2);
20 if(ch1 == ch3 && ch2 != ch3){
21 p1++;
22 p3++;
23 }else if(ch1 != ch3 && ch2 == ch3){
24 p2++;
25 p3++;
26 }else if(ch1 != ch3 && ch2 != ch3){
27 return false;
28 }else{
29 return isInterleaveCore(s1 , p1 + 1 , s1End , s2 , p2 , s2End , s3 , p3 + 1 , s3End) ||
30 isInterleaveCore(s1 , p1 , s1End , s2 , p2 + 1, s2End , s3 , p3 + 1 , s3End);
31 }
32 }else if(p1 <= s1End){
33 char ch1 = s1.charAt(p1);
34 if(ch1 != ch3) return false;
35 else{
36 p1++;
37 p3++;
38 }
39 }else if(p2 <= s2End){
40 char ch2 = s2.charAt(p2);
41 if(ch2 != ch3) return false;
42 else{
43 p2++;
44 p3++;
45 }
46 }
47 }
48 return true;
49 }

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

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

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

[leetcode]_Interleaving String,布布扣,bubuko.com

时间: 2024-12-17 02:06:27

[leetcode]_Interleaving String的相关文章

leetcode第一刷_Interleaving String

有关这种字符串的题真是层出不穷啊,而且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比较好写的,用ispart[i][j]代表s1贡献i长,s2贡献j长时,能不能形成s3的前i+j个字符.更新可以按照行或者列开始,s3的前i+j个字符,可以是((i-1)+1)+j构成,也可以是i+((j-1)+1)构成,这取决于当前的这个字符s3[i+j-1]跟s1[i-1]和s2[j-1]是否相等,或的关系就可以解决问题.

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

LeetCode: Scramble String

LeetCode: Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble t

LeetCode: Interleaving String

LeetCode: Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc"

[leetcode]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.Whe

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

[LeetCode] Scramble String(树的问题最易用递归)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,