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. tcp三次握手和四次握手的理解

    三次握手:发生在建立tcp的时候 1.客户端:发送一个syn包给服务端(同步) 2.服务端:发送一个ack包再加一个syn包给客户端(应答+同步) 3.客户端:发送一个ack包给服务端(应答) 四次握 ...

  2. MongoDB dataSize如何比storageSize更大?

      原文   https://stackoverflow.com/questions/34054780/how-can-mongodb-datasize-be-larger-than-storages ...

  3. Kubernetes 学习25 创建自定义chart及部署efk日志系统

    一.概述 1.我们说过在helm架构中有这么几个关键组件,helm,tiller server,一般托管运行于k8s之上,helm能够通过tiller server在目标k8s集群之上部署应用程序,而 ...

  4. DOS窗口操作MySQL数据库

    本周学习内容: 1.学习MySQL数据库.Linux私房菜: 2.等级评测培训: 3.练习MySQL数据库.练习CentOS7: 实验内容: 1.使用DOS窗口进入MySQL数据库 2.解决MySQL ...

  5. learning java Random 和 ThreadLocalRandom类

    var rand = new Random(); System.out.println(rand.nextBoolean()); System.out.println(rand.nextInt()); ...

  6. shell脚本的参数传递使用

    1.params.sh源码如下 #!/bin/bash # author:daokr # url:www.daokr.com echo "Shell 传递参数实例!"; echo ...

  7. lixuxmint系统定制与配置(3)-字体

    小书匠Linux 有些系统自带的字体实在太难看了,看起来不清晰,不明确,有一个好的字体,可以带来好心情,并提高工作与效率. 1.常用中文字体 文泉驿微黑,微软雅黑,思源黑体 2.字体安装 2.1检查已 ...

  8. Go工程项目方面注意

    1.同一个文件夹下的包名必须相同 2.文件夹下go文件使用的包名不是必须同文件夹名,但建议包名同文件夹名 3.不用目录包名不同 4.调用不同包里面的函数格式:包名.函数名(...) 5.包导出给外部使 ...

  9. Python正则表达式【转载】

    原作者:LouieZhang 原文出处:https://www.cnblogs.com/LouieZhang/p/7399836.html 0x00 简介 正则表达式就是描述字符串排列的一套规则.利用 ...

  10. Bootstrap select 多选并获取选中的值

    代码: <!DOCTYPE html><html> <head>    <meta charset="UTF-8">    < ...