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

代码如下:

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. 关于html自闭合标签要不要加空格和斜杠的问题?

    问题描述:可能很多人都遇到过这个问题,写网页时,link img br input等等这些标签时到底要不要在结尾加上空格和斜杠呢? 我曾经貌似在<编写高质量代码>上看到过这样的介绍,遇到l ...

  2. 基于springmvc和restClient的rest服务的测试

    在开发中,不熟悉驱动驱动测试开发的coder往往喜欢将服务应用启动以后测试,对于GET请求可以直接在浏览器中输入URL就可以,参数非中文可以直接追加到URL后.但是,对于其他请求方式的测试必须借助相应 ...

  3. python---常见的数据队列

    一.FIFO队列(先进先出) #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'zhoufeng' #FIFO队列 import q ...

  4. cocos2d-js去掉左下角的三行数字(帧数)

    project.json 里面"showFPS": true, 改成 false 就行了... 调整帧率也在这里调整 或者是 同cocos2dx,cocos2d-js左下角的FPS ...

  5. Struts2和Spring的整合

    1.Struts和Spring之间的整合,还是配置问题. 2.最重要的是spring是个容器,原来所有的框架的使用是要注入到spring中的啊.... 怪不得,说它是个容器那,原来还真是个容器啊! 3 ...

  6. sql搜索like通配符的用法详解

    http://www.lmwlove.com/ac/ID878 有很多朋友写了几年的like搜索,可能对like后面通配符的用法都不了解,甚至于%的作用是什么都不清楚.在这篇文章中,我们就一起来学习一 ...

  7. PostgreSQL9.2安装和配置指南

    本文只介绍PostgreSQL9.2在centos上的安装和配置过程 1.执行yum 命令安装PostgreSQL yum install postgresql-server 2.初始化Postgre ...

  8. List<Object> to JSONArray一

    package com.beijxing.TestMain; import java.util.ArrayList; import java.util.List; import com.beijxin ...

  9. C语言中的回调函数(Callback Function)

    1 定义和使用场合 回调函数是指 使用者自己定义一个函数,实现这个函数的程序内容,然后把这个函数(入口地址)作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数.函数是你实现 ...

  10. Ptex源码学习笔记-2

    写入纹理数据: 主要分为五种写入方式:新建纹理.编辑已有纹理.编辑ExtHeader中的指定项.写入元数据和写入指定面的纹理数据.写入过程中数据存在一个临时文件中,在close时才会把临时文件的内容拷 ...