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 * ...
随机推荐
- Delphi2010中DataSnap高级技术(7)—TDSServerClass中Lifecycle生命周期三种属性说明
Lifecycle 三种属性: Session.Invocation.Server 这三种属性都用在什么情况,有什么要注意的事项,Delphi2010中罕有说明. 如果乱用这三种属性,你的服务程序有可 ...
- Redis从基础命令到实战之集合类型(Set)
Redis集合类型的基础功能也是存储字符串列表,和列表类型的区别是字符串不能重复且没有顺序.当然,存储元素唯一性也可以通过应用程序保证,单从这一点上并没有体现出对比列表类型的特点. 其实,集合类型的一 ...
- 关于C#程序无故退出
今天我发现一种情况,分享下 我一个对象是用多线程写的代码,主程序调用完后有时候也会退出,catch不到.我在原对象的接口里面加上lock之后就ok了!我的理解是该对象申请的资源没释放完毕,加lock后 ...
- 机器学习--Classifier comparison
最近在学习机器学习,学习和积累和一些关于机器学习的算法,今天介绍一种机器学习里面各种分类算法的比较 #!/usr/bin/python # -*- coding: utf-8 -*- "&q ...
- AngulaJS路由 ui-router 传递多个参数
定义路由: .state('txnresult', { url: '/txnresult/:originAmount/:finalAmount/:currentPoint/:txnId/:discou ...
- Linux性能工具介绍
l Linux性能工具介绍 p CPU高 p 磁盘I/O p 网络 p 内存 p 应用程序跟踪 l 操作系统与应用程序的关系比喻为“唇亡齿寒”一点不为过 l 应用程序的性能问题/功能问 ...
- Android 学习 (一)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...
- EXISTS语句
通常在我写EXISTS语句时,我会写成IF EXISTS(SELECT TOP(1) 1 FROM XXX),也没细细考究过为什么要这么写,只是隐约认为这样写没有啥问题,那今天就深究下吧! 首先准备测 ...
- iOS开发】canOpenURLl 和修改http请求
控制台输出 如图是在我启动一个 Xcode + iOS 的 App 之后,控制台的输出. 这在 Xcode 时,是不会有的情况,原因是[为了强制增强数据访问安全, iOS9 默认会把所有从NSURLC ...
- Android开发学习---template requires a minimum SDK version of at least 7,build target API version of 14
adt 22.6.3的bug 当adt更新到22.6.3,其编辑器中最低支持api7,即android 2.1,这里可能是google故意这么做的,也可能是其bug.其target sdk 和comp ...