Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

详见:https://leetcode.com/problems/regular-expression-matching/description/

方法一:

class Solution {
public:
bool isMatch(string s, string p) {
if(p.empty())
{
return s.empty();
}
if(p.size()==1)
{
return (s.size()==1&&(s[0]==p[0]||p[0]=='.'));
}
if(p[1]!='*')
{
if(s.empty())
{
return false;
}
return (s[0]==p[0]||p[0]=='.')&&isMatch(s.substr(1),p.substr(1));
}
while(!s.empty()&&(s[0]==p[0]||p[0]=='.'))
{
if(isMatch(s,p.substr(2)))
{
return true;
}
s=s.substr(1);
}
return isMatch(s,p.substr(2));
}
};

方法二:动态规划,dp[i][j] 表示 s[0..i] 和 p[0..j] 是否 match

class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); dp[0][0] = true;
for (int i = 1; i <= m; i++)
{
dp[i][0] = false;
}
for (int j = 1; j <= n; j++)
{
dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];
} for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] != '*')
{
dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
}
else
{
dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && dp[i - 1][j];
}
}
} return dp[m][n];
}
};

参考:http://www.cnblogs.com/grandyang/p/4461713.html

010 Regular Expression Matching 正则表达式匹配的更多相关文章

  1. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  3. [LeetCode]10. Regular Expression Matching正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for ...

  4. 10. Regular Expression Matching正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  6. No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  7. LeetCode--No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  8. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. 10. Regular Expression Matching字符串.*匹配

    [抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. nginx用cookie控制访问权限实现方法

    自己的一个需求需要对a.b.com 下的 /c 这个目录下,cookie d=e 才能访问,如果不是,就重定向到f.html 下面看代码.  代码如下 复制代码 server{       serve ...

  2. js基础之变量类型

    1.NAN(Not a number) 不是一个数字 自身:console.log(NaN==NaN)和console.log(NaN===NaN)返回值都是false; 其他函数,isNaN()可用 ...

  3. openssh for windows

  4. Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类

    1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...

  5. go 语言 基础 类型(1)

    变量 使用关键字 var定义变量,自动初始化为0值.如果提供初始化值,可省略变量类型,由编译器自动推断. 在函数内部可以使用 := 方式定义变量 func main() { x := 123 } 可一 ...

  6. 左连接,右连接,内连接,Union

    数据库的三种常用连接解析: 官方解释: 1.left [outer] join(左外联接) 返回 包括左表中的所有记录和右表中联结字段相等的记录 2.right [outer] join(右外联接) ...

  7. C笔试题(一)

    a和b两个整数,不用if, while, switch, for,>, <, >=, <=, ?:,求出两者的较大值. 答案: int func(int a, int b) { ...

  8. js数组中的reverse()方法

    reverse方法是将数组中的元素的顺序进行反转,在原数组上操作,然后返回原数组.由于本人是学习js的新人,对reverse函数进行了几个小实验,以下实验均在Chrome浏览器上运行 实验一:reve ...

  9. [413D][搜索]D - Field expansion

    http://codeforces.com/contest/799/problem/D 解题关键:因为3^11>100000,所以若只把2单独拿出,最多只需要暴力2^11次,故只需要dfs一下即 ...

  10. Ubuntu12.04安装svn1.8

    先在终端执行sudo sh -c 'echo "# WANdisco Open Source Repo" >> /etc/apt/sources.list.d/WANd ...