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

代码如下:

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. js监听浏览器,关闭,刷新

    //浏览器关闭或刷新事件 function bindCloseBrowser() { var a = "注意!!\n您即将离开页面!离开后可能会导致数据丢失\n\n您确定要离开吗?" ...

  2. javascript判断iphone/android手机横竖屏模式的函数

    function orientationChange(){ switch(window.orientation) { case 0: // Portrait case 180: // Upside-d ...

  3. git和nginx安装

    原始地址: https://www.zybuluo.com/freeethy/note/192109 git安装 设置git的username和email (注册gitlab的账号密码) $$ git ...

  4. 使用英文版eclipse保存代码,出现some characters cannot be mapped using "Cp1251" character encoding.

    some characters cannot be mapped using "Cp1251" character encoding. 解决办法:方案一: eclipse-> ...

  5. delphi中break,continue, exit,abort, halt, runerror的异同

    delphi中表示跳出的有break,continue, exit,abort, halt, runerror. 1.break 强制退出循环(只能放在循环中),用于从For语句,while语句或re ...

  6. SDH误码仪MP1570A的自动化

    MP1570A是日本安立公司的用于SDH测试的误码仪. 1.MP1570A的自动化测试场景和原理 任意测试PC--(telnet)-->测试PC(Tcl Interrupt)-->SIG_ ...

  7. 关于context:component-scan配置中use-default-filters参数的作用

    参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...

  8. 《CoffeeScript应用开发》学习: CoffeeScript高级用法

    正确的上下文 使用胖箭头=>表示将回调函数绑定到this对象. class t func: (callback)-> if callback? setTimeout callback(), ...

  9. 关于spring AOP的学习

    比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html

  10. uml的四种关系

    UML的四种常用关系: 泛化关系.关联关系.实现关系.依赖关系 其中泛化关系是指父类与子类之间的继承关系: 实现关系是指接口与实现类之间的关系: 依赖关系和关联关系的区别如下: 只要存在对象间的交互, ...