一天一道LeetCode系列

(一)题目

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).

The function prototype should be: bool isMatch(const char *s, const

char *p)

Some examples:

isMatch(“aa”,”a”) → false

isMatch(“aa”,”aa”) → true

isMatch(“aaa”,”aa”) → false

isMatch(“aa”, “a*”) → true

isMatch(“aa”,”.*”) → true

isMatch(“ab”, “.*”) → true

isMatch(“aab”, “c*a*b”) → true

(二)解题

本题的意思是实现正则表达式的判断函数。

tips:评级为hard的题还真是难做!

考虑到aaaaaab和a*b,这种无法判断*的结束位置,需要用到动态规划来求解。

分为以下两种情况:

case 1:p[j+1] !=’*’时,无论是是s[i] == p[j]还是p[j]==’.’,状态转移方程均为dfs[i][j] = dfs[i+1][j+1];

case 2:p[j+1] == ‘‘时,这个时候就需要判断匹配的结束位置。

    while(*s == *p || *s != '\0' && *p == '.')
    {
        if(Match(s,p+2)) return true;
        s++;
    }

结束位置找到以后状态转移方程为:dfs[i][j] = dfs[i][j+2];

接下来看代码:

    class Solution {
    public:
        bool isMatch(string s, string p) {
           Match((const char *)&s[0],(const char *)&p[0]);
        }
        bool Match(const char *s,const char *p)
        {
            if(*p == '\0') return *s == '\0';
            if(*(p+1) == '*')
            {
                while(*s == *p || *s != '\0' && *p == '.')
                {
                    if(Match(s,p+2)) return true;
                    s++;
                }
                return Match(s,p+2);
            }
            else
            {
                if(*s == *p ||*s != '\0' && *p == '.')
                {
                    return Match(s+1,p+1);
                }
                return false;
            }
        }
    };

【一天一道LeetCode】#10. Regular Expression Matching的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

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

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

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. [LeetCode] 10. Regular Expression Matching

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

  7. Java [leetcode 10] Regular Expression Matching

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

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

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

  9. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

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

  10. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

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

随机推荐

  1. Linux 下的一个全新的性能测量和调式诊断工具 Systemtap, 第 3 部分: Systemtap

    Systemtap的原理,Systemtap与DTrace比较,以及安装要求和安装步骤本系列文章详细地介绍了一个Linux下的全新的调式.诊断和性能测量工具Systemtap和它所依赖的基础kprob ...

  2. 【SSH系列】spring中为什么要使用IOC

    开篇前言 在前面的博文中,小编主要简单的介绍了spring的入门知识,随着学习的深入,我们知道spring最核心的两大技术,IOC和AOP,这两个技术也是spring最耀眼的地方,在后续的博文中小编将 ...

  3. MongoDB实用教程

    ---------------------------------------------------------------------------------------------------- ...

  4. rbac数据库设计

    1 rbac数据库设计 RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制分享牛原创,分享牛系列,分享牛.rbac 用户角色权限资源表如 ...

  5. TOP-N类查询

    Top-N查询 --Practices_29:Write a query to display the top three earners in the EMPLOYEES table. Displa ...

  6. PGM:贝叶斯网表示之朴素贝叶斯模型naive Bayes

    http://blog.csdn.net/pipisorry/article/details/52469064 独立性质的利用 条件参数化和条件独立性假设被结合在一起,目的是对高维概率分布产生非常紧凑 ...

  7. Hibernate缓存集成IMDG

    1 第三方缓存插件 除了Ehcache这种轻量级的缓存方案外,几乎所有IMDG产品都提供了对Hibernate二级缓存的直接支持,常用的有: Ø  Hazelcast Ø  GridGain Ø  J ...

  8. UE4成批处理透明材质

    项目中需要控制成批的物体的透明度,但是默认的时候他又不能是透明的,对,项目的要求就这么诡异. 然而却没有找到设置材质的BlendMode的功能,于是只有换了一种办法,物体需要透明时更换为透明材质,默认 ...

  9. 【移动开发】binder阻塞/非阻塞与单向/双向的问题

    The client thread calling transact is blocked by default until onTransact has finishedexecuting on t ...

  10. Android简易实战教程--第七话《在内存中存储用户名和密码》

    首先是配置文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...