'.' 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

以下是牛人的实现:

class Solution {
public:
bool isMatch(string s, string p) {
int i, j;
int m = s.size();
int n = p.size();
const char *s1 = s.c_str();
const char *p1 = p.c_str();
/**
* b[i + 1][j + 1]: if s[0..i] matches p[0..j]
* if p[j] != '*'
* b[i + 1][j + 1] = b[i][j] && s[i] == p[j]
* if p[j] == '*', denote p[j - 1] with x,
* then b[i + 1][j + 1] is true iff any of the following is true
* 1) "x*" repeats 0 time and matches empty: b[i + 1][j -1]
* 2) "x*" repeats 1 time and matches x: b[i + 1][j]
* 3) "x*" repeats >= 2 times and matches "x*x": s[i] == x && b[i][j + 1]
* '.' matches any single character
*/
bool b[m + ][n + ];
b[][] = true;
for (i = ; i < m; i++)
{
b[i + ][] = false;
}
// p[0..j - 2, j - 1, j] matches empty iff p[j] is '*' and p[0..j - 2] matches empty
for (j = ; j < n; j++)
{
b[][j + ] = j > && '*' == p1[j] && b[][j - ];
} for (i = ; i < m; i++)
{
for (j = ; j < n; j++)
{
if (p[j] != '*')
{
b[i + ][j + ] = b[i][j] && ('.' == p1[j] || s1[i] == p1[j]);
}
else
{
b[i + ][j + ] = b[i + ][j - ] && j > || b[i + ][j] ||
b[i][j + ] && j > && ('.' == p1[j - ] || s1[i] == p1[j - ]);
}
}
}
return b[m][n];
} };

这应该是用动态规划的方法实现的。

LeetCode Regular Expression Matching 网上一个不错的实现(非递归)的更多相关文章

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

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

  2. [leetcode]Regular Expression Matching @ Python

    原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...

  3. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  4. [LeetCode] Regular Expression Matching [6]

    称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  5. [LeetCode] Regular Expression Matching(递归)

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

  6. LeetCode——Regular Expression Matching

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

  7. Leetcode:Regular Expression Matching分析和实现

    题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...

  8. LeetCode: Regular Expression Matching 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

  9. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

随机推荐

  1. Ubuntu下使用Git_2

    接着上一篇的写,这里练习一下git clone 指令 指令格式 $ git clone <repository> <directory> <respository> ...

  2. C++学习007-使用exit退出进程

    使用exit可以实现退出当前进程. 如下 在程序接收到一个字符后,就退出进程 编写环境 vs2015 int main() { int a = 10, b = 20; std::cout <&l ...

  3. 虚拟现实-VR-UE4-认识UE4

    VR的火热,让每个人都想参与一下, 公司在展会上面搞了一个VR的Demo,关注度超出预期,使得公司高层决定来个VR项目 所以 关于UE4 百度百科地址:http://baike.baidu.com/l ...

  4. resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)

    #!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...

  5. kaldi常用文件查看指令

    目录 1. ark特征文件 2. FST文件 3. mdl模型文件 4. 决策树文件 5. ali.gz对齐文件 资料来自kaldi官方文档. 转载注明出处. 1. ark特征文件 copy-feat ...

  6. npm无法安装全局web3的问题

  7. 【转】 The user specified as a definer ('root'@'') does not exist when using LOCK TALBE

    在linux下,用mysql的导出语句: mysqldump -u root -pPasswd table >/home/lsf/test.sql 出现了 Got error: 1449: Th ...

  8. linux系统基础文件属性

    记录用户登录前显示的信息 cat /etc/issue vim  /etc/motd    设置登录提醒 隐藏执行命令的历史记录用history –d  加上历史记录行号 如history -d 38 ...

  9. shit element ui & form password validation

    shit element ui & form password validation shit docs https://github.com/yiminghe/async-validator ...

  10. input只改变光标的颜色 不改变字的颜色

    color: red; text-shadow: 0px 0px 0px #000; -webkit-text-fill-color: transparent;把这些放到input里文字通过阴影实现 ...