'.' 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. C#文件重命名的代码

    C#中没有重命名的方法,自己写了一个方法,来处理文件的重命名. /// <summary> /// 重命名文件夹内的所有子文件夹 /// </summary> /// < ...

  2. Python进程、线程、协程及IO多路复用

    详情戳击下方链接 Python之进程.线程.协程 python之IO多路复用

  3. Ubuntu16.04安装Zabbix

    基于Zabbix+MySQL+Apache(可选) apt-get install php7.0-bcmath php7.0-xml php7.0-mbstring安装Zabbix所需的几个PHP模块 ...

  4. VMware快照

    越来越多的人喜欢使用虚拟机来做实验,但是实验过程并不总是顺利的,所以我们就需要掌握虚拟机快照的使用方法,个人建议的顺序为: 1 在虚拟机打开之前,点击“虚拟机”--"快照"--&q ...

  5. Linux系统安装jdk后出现无法执行binary 文件的错误解决

    这是由于jdk版本的问题,针对Linux系统,Oracle提供了 ARM 的32jdk和64位jdk , 但是也提供了类似这样jdk-8u191-linux-i586.tar.gz32或64位的jdk ...

  6. Mysql性能优化四:分库,分区,分表,你们如何做?

    分库分区分表概念 分区 就是把一张表的数据分成N个区块,在逻辑上看最终只是一张表,但底层是由N个物理区块组成的 分表 就是把一张数据量很大的表按一定的规则分解成N个具有独立存储空间的实体表.系统读写时 ...

  7. C++STL——map

    一.相关定义 map 关联容器,存储相结合形成的一个关键值和映射值的元素 提供一对一(第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可以称为该关键字的值)的数据处理能力 map对象是模 ...

  8. C#的Response.BinaryWrite图片乱码问题

    今天学习Response对象,该对象的有很多的输出方式,其中有一个binaryWrite可以输出图片,但是在输出图片一开始出现了乱码,后来通过百度得到解决: 代码: FileStream stream ...

  9. iOS-UIImageView播放动画

    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"lanya1"],[UIImage imag ...

  10. window下对samba的清理操作

    windows清除访问samba局域网密码缓存 1.在dos窗口中输入control userpasswords2或者control keymgr.dll,然后[高级]/[密码管理],删掉保存的该机器 ...