递归方法运行时间过长。考虑使用动态规划的方法。

代码如下:

bool isMatch(string s, string p) {
int i,j;
int m=s.size();
int n=p.size();
bool b[m + ][n + ]; // b[i][j]表示s[i-1]和p[j-1]的匹配结果
b[][] = true;
for (i = ; i < m; i++) {
b[i + ][] = false;
} for (j = ; j < n; j++) {
b[][j + ] = j > && '*' == p[j] && b[][j - ];
} for (i = ; i < m; i++) {
for (j = ; j < n; j++) {
if (p[j] != '*') {
b[i + ][j + ] = b[i][j] && ('.' == p[j] || s[i] == p[j]); //表达式中的b[i][j]的意思是:s[i-1]已经和p[j-1]匹配。下同。
} else {
b[i + ][j + ] = b[i + ][j - ] && j > || b[i + ][j] ||
b[i][j + ] && j > && ('.' == p[j - ] || s[i] == p[j - ]); //别分为*之前的字符匹配零次、一次、多次
}
}
}
return b[m][n];
}

Regular Expression Matching leetcode的更多相关文章

  1. Regular Expression Matching leetcode java

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

  2. lc面试准备:Regular Expression Matching

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

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

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

  4. LeetCode | Regular Expression Matching

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

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  7. [LeetCode][Python]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  8. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  9. [leetcode]Regular Expression Matching @ Python

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

随机推荐

  1. uva11292 Dragon of Loowater

    水题,排序遍历即可 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  2. CentOS 6下Apache的https虚拟主机实践

    题目:1.建立httpd服务器,要求: 提供两个基于名称的虚拟主机: (a)www1.buybybuy.com,页面文件目录为/web/vhosts/www1:错误日志为/var/log/httpd/ ...

  3. Unity3D 学习——入门资料整理

    感觉可以写3D游戏,还是用C#,很好玩的样子.整理最近学习资料 介绍:游戏引擎,可以用C#配置的游戏引擎.嗯,就这样.其余度娘. 安装:官网,因为现在个人版是免费的.直接官网下.干净利落. 调试插件: ...

  4. mysql 导出csv

    SELECT order_id,product_name,qty FROM ordersINTO OUTFILE '/tmp/orders.csv'FIELDS TERMINATED BY ','EN ...

  5. HBase的伪分布式安装(原创)

    准备工作: 1)安装了伪分布式hadoop:参照http://blog.csdn.net/zolalad/article/details/11472207 2)修改已安装好的hadoop配置文件: a ...

  6. 第六百零九天 how can I 坚持

    好失败啊,搞了一天,竟然连个环境都没搞好,也是醉了.还是太渣了. 洗澡,睡觉.

  7. python字符串及其方法详解

    首先来一段字符串的基本操作 str1="my little pony" str2="friendship is magic" str3=str1+", ...

  8. iOS强制屏幕旋转

    /** 强制旋转屏幕为纵向 (注:这种方式 键盘不能旋转过来; iOS8.x下 UIAlterView旋转不过来  ) @return */ + (void)rotateOrientationPort ...

  9. Java:基于LinkedList实现栈和队列

    1.提供一组栈的接口,其底层关联到一个LinkedList(双端队列)实例.由于只暴露部分基于栈实现的接口,所以可以提供安全的栈实现. package junit; import java.util. ...

  10. CentOS 7 安装php开发环境

    安装服务 : yum install httpd httpd-devel  service httpd start 启动     安装mariadb : yum -y install mariadb* ...