• 正则表达式匹配


    正则表达式匹配

    题目描述

    请实现一个函数用来匹配包括'.'和''的正则表达式。模式中的字符'.'表示任意一个字符,而''表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配

    剑指offer版

    class Solution {
    public:
        bool matchCore(char* str, char* pattern) {
            if ((*str == '') && (*pattern == ''))    // 都比较到最后一个字符时
                return true;
            if ((*str != '') && (*pattern == ''))    // str没有结束, 但pattern结束时
                return false;
            
            if (*(pattern + 1) == '*') {        // 第二个字符是*
                if ((*str == *pattern) || (*str != '' && *pattern == '.'))    // 匹配一个字符
                    return matchCore(str+1, pattern)        //匹配到1个字符, 模式不变, 如"aa","a*"
                        || matchCore(str, pattern+2);      // 匹配到0个字符, 模式+2,   如"bbbba",".*a*a"
                        //|| matchCore(str+1, pattern+2);    // 发现这个可以注释掉
                else        // 没有匹配到字符
                    return matchCore(str, pattern+2);
            }
            
            if ((*str == *pattern) || (*str != '' && *pattern == '.'))    // 第二个字符不是*
                return matchCore(str+1, pattern+1);
            
            return false;
        }
        
        bool match(char* str, char* pattern)
        {
            if ((str == nullptr) || (pattern == nullptr))
                return false;
            return matchCore(str, pattern);
        }
    };
    
  • 相关阅读:
    Java mysql数据库连接Demo1
    java JDBC编程流程步骤
    spring boot定时任务
    mysql分页
    mysql limit 偏移量过大效率解决方式 转贴
    svn both sides of the move must be committed together
    An invalid property 'jdbcType ' was found in mapping
    JSON高亮格式化页面显示
    nprogress 转
    org.apache.commons工具类方法解释 转
  • 原文地址:https://www.cnblogs.com/hesper/p/10548460.html
Copyright © 2020-2023  润新知