[LeetCode] Regular Expression Matching

1. 只要求匹配带‘.‘和‘*‘

2. ‘.‘可以当作任何字符来匹配

3. 只有‘*‘需要特殊处理,‘*‘可以匹配0个或者多个前面的字符。 注意处理好边界情况。

class Solution {
public:
    bool isMatch(string s, string p) {
        int ans = 0;
        int n = s.size();
        int m = p.size();
        vector<vector<int>> visit(n+1, vector<int>(m+1));
        isMatch(s, p, 0, 0, visit, ans);
        return ans;
    }

    void isMatch(const string& s, const string& p, int u, int v, vector<vector<int>>& visit, int& ans) {
        if (visit[u][v]) return;
        visit[u][v] = 1;
        if (ans) return;
        if (u == s.size() && v == p.size()) {
            ans = 1;
            return;
        }

        if (p[v] == ‘*‘) {
            isMatch(s, p, u, v+1, visit, ans);
            return;
        }

        if (v+1 < p.size() && p[v+1] == ‘*‘) {
            // zero match
            isMatch(s, p, u, v+1, visit, ans);
        }

        if (u == s.size() || v == p.size()) return;
        if (p[v] == ‘.‘ || p[v] == s[u]) {
            isMatch(s, p, u+1, v+1, visit, ans);
            if (v+1 < p.size() && p[v+1] == ‘*‘) {
                isMatch(s, p, u+1, v, visit, ans);
            }
        }
    }
};

  

时间: 2024-11-05 05:57:02

[LeetCode] Regular Expression Matching的相关文章

[LeetCode] Regular Expression Matching(递归)

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

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

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

leetcode——Regular Expression Matching

题目: 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 shoul

LeetCode -- Regular Expression Matching 【算法】

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

LeetCode Regular Expression Matching 网上一个不错的实现(非递归)

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

LeetCode: Regular Expression Matching 解题报告

Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. SOLUTION 1: 思路: 从后往前遍历罗马数字,如果某个数比前一个数小,则把该数在结果中减掉:反之,则在结果中加上当前这个数: GITHUB 代码: https://github.com/yuzhangcmu/LeetCode_algorithm

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

[LeetCode][Python]Regular Expression Matching

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin

[LEETCODE][PYTHON][DP]REGULAR EXPRESSION MATCHING

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin