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. Longest Continuous Increasing Subsequence II

    Description Given an integer matrix. Find the longest increasing continuous subsequence in this matr ...

  2. DW-ODS

    ODS (操作数据存储) 编辑 讨论 操作数据存储ODS(Operational Data Store)是数据仓库体系结构中的一个可选部分,也被称为贴源层.ODS具备数据仓库的部分特征和OLTP系统的 ...

  3. 计蒜客模拟赛 #5 (B 题) 动态点分治+线段树

    虽然是裸的换根dp,但是为了在联赛前锻炼码力,强行上了点分树+线段树. 写完+调完总共花了不到 $50$ 分钟,感觉还行. code: #include <bits/stdc++.h> # ...

  4. ehcache.xml 配置文件备忘录(不建议出现中文注释,此处备忘)

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  5. css+vue实现流程图

    主要用css+flex布局实现样式部分,vue实现组件逻辑.首先看下效果吧: 当空间不够时还可以使用拖拽功能 接下来说明下实现思路 1.首先是实现单个节点样式,这个很简单不谈了,节点后都跟有一小段连接 ...

  6. [CMS]Joomla 3.4.6-RCE漏洞复现

    0x00:简介 1.Joomla是一套全球有名的CMS系统. 2.Joomla基于PHP语言加上MySQL数据库所开发出来的WEB软件系统,目前最新版本是3.9.12. 3.Joomla可以在多种不同 ...

  7. 从一个表中往另外一个表中插入数据用到的SQL

    insert into jdjc_zzjcxm (zj,jcxmmc) select sys_guid(),zbmc from JDJC_WHJXXMMC;

  8. 记一次有惊无险的 JVM 优化经历

    转载:https://my.oschina.net/u/3627055/blog/2995973 背景 生产环境有二台阿里云服务器,均为同一时期购买的,CPU.内存.硬盘等配置相同.具体配置如下: 节 ...

  9. vue+elementui搭建后台管理界面(5递归生成侧栏路由)

    有一个菜单树,顶层菜单下面有多个子菜单,子菜单下还有子菜单... 这时候就要用递归处理 1 定义多级菜单 修改 src/router/index.js 的 / 路由 { path: '/', redi ...

  10. php手记之06-tp5验证器

    # 创建验证器 php think make:validate 模块名/验证器名(首字母大写) # 验证器 namespace app\index\validate; use think\Valida ...