10. Regular Expression Matching

自己写了一下午,总是考虑不全,看了一个版本,使用递归,豁然开朗

class Solution {
public:
    bool isMatch(string s, string p) {
        if (p.empty())    return s.empty();

        if (‘*‘ == p[1])
            // x* matches empty string or at least one character: x* -> xx*
            // *s is to ensure s is non-empty
            return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || ‘.‘ == p[0]) && isMatch(s.substr(1), p));
        else
            return !s.empty() && (s[0] == p[0] || ‘.‘ == p[0]) && isMatch(s.substr(1), p.substr(1));
    }
};
时间: 2024-08-07 02:26:12

10. Regular Expression Matching的相关文章

10. Regular Expression Matching && 44. Wildcard Matching

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

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

10. Regular Expression Matching(hard)

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

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

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

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】10.Regular Expression Matching

题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串

LeetCode (10): Regular Expression Matching [HARD]

https://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 matching should cover the ent

leetcode problem 10 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 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 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 entir