LeetCode(97) 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", return false.
分析
AC代码
class Solution {
public:
/*方法一:递归实现,对大数据组会TLE*/
bool isInterleave1(string s1, string s2, string s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
if (len2 == 0)
return s1 == s3;
else if (len1 == 0)
return s2 == s3;
else if (len3 == 0)
return len1 + len2 == 0;
else
{
if (s1[0] == s3[0] && s2[0] != s3[0])
return isInterleave1(s1.substr(1), s2, s3.substr(1));
else if (s1[0] != s3[0] && s2[0] == s3[0])
return isInterleave1(s1, s2.substr(1), s3.substr(1));
else if (s1[0] == s3[0] && s2[0] == s3[0])
return isInterleave1(s1.substr(1), s2, s3.substr(1)) || isInterleave(s1, s2.substr(1), s3.substr(1));
else
return false;
}//else
}
/*方法二:二维动态规划*/
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
if (len1 + len2 != len3)
return false;
else if (len2 == 0)
return s1 == s3;
else if (len1 == 0)
return s2 == s3;
else if (len3 == 0)
return len1 + len2 == 0;
else
{
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= len1; ++i)
{
if (s1[i - 1] == s3[i - 1])
dp[i][0] = 1;
else
break;
}//for
for (int j = 1; j <= len2; ++j)
{
if (s2[j - 1] == s3[j - 1])
dp[0][j] = 1;
else
break;
}//for
for (int i = 1; i <= len1; ++i)
{
for (int j = 1; j <= len2; ++j)
{
if (s1[i - 1] == s3[i + j - 1])
dp[i][j] = dp[i - 1][j] || dp[i][j];
if (s2[j - 1] == s3[i + j - 1])
dp[i][j] = dp[i][j - 1] || dp[i][j];
}//for
}//for
return dp[len1][len2] == 1;
}//else
}
};
LeetCode(97) Interleaving String的更多相关文章
- LeetCode(97):交错字符串
Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- char型字符串(数组)与string型字符串 指针与引用
一.常指针: int *const p; //指针不可改变,但是指针指向的数据可以改变. 指向常量的指针: const int *p; //指针可以改变,但是指针指向的数据不可以改变. 指 ...
- JAVA基础学习之路(九)[2]String类常用方法
字符与字符串: 1.将字符数组变为字符串(构造方法) public String(char[] value) Allocates a new String so that it represents ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- Python:利用内建函数将字符串转化为整数
使用内建函数raw_input()内建函数,它读取标准输入,并将读取到的数据赋值给指定的变量.我们可以使用int()内建函数将用户输入的字符串转换为整数: >>> user = ra ...
- scala-尾递归
------------------------- by chenkh ----------------------------- 随笔记录什么是尾递归,为什么需要尾递归,尾递归show by exa ...
- python 获取一个列表有多少连续列表
python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:
- SQL中判断一串字符中是否有特定的字符
),) SET @s='1,2,3,4,5,6,7,8,9,10' 一:SET @sql='select col='''+ replace(@s,',',''' union all select '' ...
- MFC CEdit 自动换行功能
最近在写一个程序,对话框上的CEdit控件需显示一串字符,字符可能比较长,要根据编辑框的宽度自动换行.控件属性中已经设置了Multiline为true.Auto VScroll为true,Virtic ...
- 策划了个.NET控件的案例大赛
任何一个产品的普及,都有一个过程: 1. 对新事物充满热情.喜欢尝鲜.或后急迫需要的人首先成为产品用户.他们总数很少,但是是产品用户的第一批种子. 2. 思想比较开放.能接受新事物的人会成为第二批用户 ...
- AS3 转 Java
不错,我就是as3转java的程序猿. 大概两年前加过as3的QQ群里,有很多群友说as3发展前景不好,很多要转语言.我当时也想转,一直苦于没机会.现在机会终于来了... 首先说明一点,as3并不会像 ...
- 修改maven默认的JDK编译版本
1.全局模式(settings.xml) <profiles> <profile> <id>jdk-1.8</id> <activation> ...
- cdnbest的站点设置里设置url跳转设置
示例: 内容示例写法: ^http://kangleweb.com/(.*)$ https://www.kangleweb.com/$1 这只是一个例子,其他用法您可以自已多试试
- Git保存密码
TortoiseGit中,通过https方式连接时,默认是不会保存帐号密码,需要我们每次输入一次,真心很麻烦! 通过简单的设置,就可以解决这一问题! 编辑仓库目录中本地的”.git/config”文件 ...