jjc

. Regular Expression Matching(hard)
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example :

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example :

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example :

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example :

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated  times, a can be repeated  time. Therefore it matches "aab".
Example :

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
Accepted ,,,
class Solution_S4ms {
public:
    bool isMatch(string s, string p) {
        ]();
        ; i < s.length() + ; ++i)
        {
            dp[i] = ];
             memset(dp[i], , p.length()+);
        }

        dp[s.size()][p.size()] = true;

        ; i--){
            ; j >= ; j--)
            {
                bool first_match = (i < s.length() &&
                                       (p[j] == s[i] ||
                                        p[j] == '.'));

                 < p.length() && p[j+] == '*')
                {
                    dp[i][j] = dp[i][j+] || first_match && dp[i+][j];
                }
                else
                {
                    dp[i][j] = first_match && dp[i+][j+];
                }
            }
        }
        ][];
    }
};

class Solution_S8ms {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<, vector<, false));
        dp[][] = true;
        ; i <= m; ++i) {
            ; j <= n; ++j) {
                 && p[j - ] == '*') {
                    dp[i][j] = dp[i][j - ] || (i >  && (s[i - ] == p[j - ] || p[j - ] == ][j]);
                } else {
                    dp[i][j] = i >  && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
                }
            }
        }
        return dp[m][n];
    }
};
/* https://www.cnblogs.com/grandyang/p/4461713.html
 * dp[i][j]表示s[0,i)和p[0,j)是否match
1.  P[i][j] = P[i - 1][j - 1], if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
2.  P[i][j] = P[i][j - 2], if p[j - 1] == '*' and the pattern repeats for 0 times;
3.  P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'), if p[j - 1] == '*' and the pattern repeats for at least 1 times.*/
class Solution_grandyang {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<, vector<, false));
        dp[][] = true;
        ; i <= m; ++i) {
            ; j <= n; ++j) {
                 && p[j - ] == '*') {
                    dp[i][j] = dp[i][j - ] || (i >  && (s[i - ] == p[j - ] || p[j - ] == ][j]);
                } else {
                    dp[i][j] = i >  && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
                }
            }
        }
        return dp[m][n];
    }
};

0010.Regular Expression Matching(H)的更多相关文章

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

    题目 Given an input string(s) and a pattern(p), implement regular expression matching with support for ...

  2. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

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

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

  4. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  5. No.010:Regular Expression Matching

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

  6. Regular Expression Matching

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

  7. 【leetcode】Regular Expression Matching

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

  8. 【leetcode】Regular Expression Matching (hard) ★

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

  9. 66. Regular Expression Matching

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

随机推荐

  1. lambda 函数的用法

    lambda函数又叫匿名函数, 匿名函数就是没有名字的函数,不使用def语句声明的函数.如果要声名,则需要使用lambda关键字进行声明. 一般用来定义简单的函数. 1.声明一个简单的加法匿名函数: ...

  2. Zookeeper数据类型、节点类型、角色、watcher监听机制

    1.Zookeeper数据类型:层次化目录结构+少量数据 Zookeeper包含层次化的目录结构,每个Znode都有唯一的路径标识,Znode可以包含数据和子节点. 其中Znode数据可以有多个版本, ...

  3. SSM整合Dubbo登陆案例

    登陆案例 一.创建Service项目存放共同数据 1.1  创建实体类 private long id; private String loginName; private String userNa ...

  4. 软件测试技术之可用性测试之WhatsApp Web

    Tag:可行性测试.测试流程.结果分析.案例分析 WhatsApp是一款面向智能手机的网络通讯服务,它可以通过网络传送短信.图片.音频和视频.WhatsApp在全球范围内被广泛使用,是最受欢迎的即时聊 ...

  5. 多线程编程threading

    操作系统能够调度和切换的最小单元实际上是线程.对于IO操作来说,多线程和多进程性能差别不大.有两种方法可以进行多线程编程. 1.使用多线程编程的两种方法 (1)直接实例化一个Thread对象 from ...

  6. Node.js创建服务及实现静态资源托管/接口请求

    1.环境 采用12.13.x版本 2.创建server.js 文件内容如下: let http = require("http"); let fs = require(" ...

  7. CentOS6.5 安装ES5.5

    一.CURL查看已开启的ES es5.5:elasticsearch-5.5.2.tar.gz下载,百度云地址 https://pan.baidu.com/s/17oFOQlePLtUhhJHxEPR ...

  8. web之大文件断点续传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  9. 力扣50题 Pow(x,n)

    本题是力扣网第50题. 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 采用递归和非递归思路python实现. class Solution: #递归思路 def myPow_recurs ...

  10. Java与设计模式之单例模式(上)六种实现方式

           阎宏博士在<JAVA与模式>中是这样描述单例模式的:作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类.      ...