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. 017_Python3 数据结构

    本章节我们主要结合前面所学的知识点来介绍Python数据结构.   *****************************   1.列表 Python中列表是可变的,这是它区别于字符串和元组的最重 ...

  2. Linux中三种SCSI target的介绍之各个target的优劣

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/scaleqiao/article/deta ...

  3. PHP查询oracle数据显示乱码问题

    1.Linux下 执行前脚本前先执行一下命令export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8" 2.Windows下在代码里添加 ...

  4. 利用nc当作备用shell管理方案.

    ssh 有时候真的就是连不上了,然后是没什么然后了呢. 或者手残改错配置然后重新sshd了. 所以这时候需要备用的远程管理工具.nc是最好的选择,一般服务器都是 内网的,如果跳板机也管理不了呢. 安装 ...

  5. Codeforces 1239E. Turtle 折半

    原文链接www.cnblogs.com/zhouzhendong/p/CF1239E.html 前言 咕了这么久之后,我的博客复活了! 题解 结论1 存在一个最优解\(A\)数组,满足\(\foral ...

  6. RHEL7下载

    RHEL7下载地址: http://mirrors.aliyun.com/centos/ 我的百度云盘地址: 链接:https://pan.baidu.com/s/1_-bm12bekFlWJiGHj ...

  7. Oracle中查询走索引的情况

    1.对返回的行无任何限定条件,即没有where子句 2.未对数据表与任何索引主列相对应的行限定条件例如:在City-State-Zip列创建了三列复合索引,那么仅对State列限定条件不能使用这个索引 ...

  8. CORS & CSP笔记

    1.CORS & CSP 浏览器跨域相关的安全策略主要存在于两个方面: 浏览器是否发送ajax 浏览器是否加载返回数据 假设从a.com 向b.com发送ajax请求.此时浏览器当前页面为a. ...

  9. #C++初学记录(遍历)

    hide handkerchief Problem Description The Children's Day has passed for some days .Has you remembere ...

  10. C前序遍历二叉树Morris Traversal算法

    首先来递归算法,简单易懂: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef ...