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. PPT扁平化设计总结

    注:以下内容基本都来自知乎,由于已经不记得网址了,所以未能附上所有相关链接,抱歉. PPT扁平化设计原则一.亲密:意思相近的内容放在一起二.对齐:页面上的某两个元素之间总是围绕一条直线对齐三.对比:有 ...

  2. linux 文件解压缩

    转载 https://blog.csdn.net/qq_27803491/article/details/52785838 01-.tar格式 解包:[*******]$ tar xvf FileNa ...

  3. springboot 2.1.6发布

    最新消息: Spring Boot 2.1.6 昨天正式发布了,日常更新一些依赖和修复一些 BUG,没什么硬菜! 重点来了,Spring Boot 1.5 将于今年 8 月结束使命,请尽快迁移到 Sp ...

  4. flutter 从接口获取json数据显示到页面

    如题,在前端,是个很简单的ajax请求,json的显示,取值都很方便,换用dart之后,除了层层嵌套写的有点略难受之外,还有对json的使用比js要麻烦 1. 可以参照 flutter-go 先封装一 ...

  5. 前端项目, 每次运行都需要输入 sudo 的解决方法

    前端项目, 每次运行都需要输入 sudo 的解决方法 node一直提示的sudo问题根本原因为: node 的所有者, 项目的所有者, 不同; 解决方法为: 将项目的所有者更改为 chown -R ` ...

  6. 整理的Python资料,包含各阶段所需网站、项目,收藏了

    虽然强调过很多次了,但是还是要多提一句,不要看python2.x,如果你是零基础过来的,请直接开始你的py3.x 之路.建议3.6,3.7的一些特性可能对你不是很重要. 1.初出茅庐 我不会推荐你们去 ...

  7. 我们使用 Kafka 生产者在发消息的时候我们关注什么(Python 客户端 1.01 broker)

    之前使用 Kafka 的客户端消费者比较多一点,而且也是无脑订阅使用也没有深入了解过具体的参数.总的来说使用不够细节. 这次公司项目活动期间暴露非常多的问题,于是有了这篇文章. 首先我们来拆解一下 K ...

  8. Loadrunner录制+运行+结果-【飞机订票系统实战】

    目录结构: 一.LoadRunner实现订票系统脚本录制 二.Loadrunner实现订票系统IP欺骗(此处可以不设置) 三.Loadrunner运行录制的脚本 四.Load generator配置 ...

  9. CSS3字体大小rem属性用法

    PX为单位 在Web页面初期制作中,我们都是使用“px”来设置我们的文本,因为他比较精确和固定. 只要页面某元素设置了px字体大小,其子元素/子孙元素未设置字体大小或设置的字体大小css优先级没父元素 ...

  10. docker中部署django项目~~Dockfile方式和compose方式

    1.  背景:   本机win10上,后端django框架代码与前端vue框架代码联调通过. 2.  目的:   在centos7系统服务器上使用docker容器部署该项目. 3.  方案一:仅使用基 ...