[LeetCode]wildcard matching通配符实现之贪心法

前天用递归LTE,昨天用动态规划LTE,今天接着搞,改用贪心法。题目再放一次:

‘?‘匹配任意字符,‘*‘匹配任意长度字符串

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
The function prototype should be:
bool isMatch(const char *s, const char *p)
思路其实更简单,和递归的有点像,两个字符串从头开始匹配,不匹配直接返回false,匹配,则两个指针都加1,若p遇到*,则s++一直到匹配到p+1,当然要记录此时p和s的位置,以便下次回溯.还是用图片说明吧:

用该方法,最差情况下复杂度为len_s*len_p,绝大情况下其实小很多.具体代码如下:
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        const char *last_s = NULL;
        const char *last_p = NULL;
        while (‘\0‘ != *s || ‘*‘ == *p)
        {
            if (*s == *p || ‘?‘ == *p)
            {
                s++;
                p++;
                continue;
            }
            if (‘*‘ == *p)
            {
                while (‘*‘ == *p)
                {
                    last_p = p;
                    p++;
                }
                while (‘\0‘ != *s && (*s != *p && ‘?‘ != *p))
                {
                    s++;
                }
                last_s = s;
                continue;
            }
            if (last_p != NULL)
            {
                p = last_p;
                s = last_s+1;
                continue;
            }
            return false;
        }
        if (‘\0‘ == *p)
        {
            return true;
        }
        return false;
    }
};

这次终于AC了.

最初的时候直观的感觉如果p中有n个*就要保存多个n个位置以回溯,其实不用,只要保证前面的子串匹配了,后面的*匹配不受影响,就是有多种匹配方案,但是只要找到一种就可以了.

这个解决方案感觉不是很美,一是思路不清爽,很容易忽略一些特殊情况,Wrong Answer了两次才成功;二是不像前两种,能解决s和p都带有通配符的情况,只能做只有p中有通配符,s中也有的情况感觉分析起来很复杂,不直观.


[LeetCode]wildcard matching通配符实现之贪心法,布布扣,bubuko.com

时间: 2024-10-24 04:25:16

[LeetCode]wildcard matching通配符实现之贪心法的相关文章

[LeetCode]wildcard matching通配符实现之动态规划

前天wildcard matching没AC,今天接着搞,改用动态规划.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*&q

[LeetCode]wildcard matching通配符实现TLE

leetcode这道题还挺有意思的,实现通配符,'?'匹配任意字符,'*'匹配任意长度字符串,晚上尝试了一下,题目如下: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching shoul

[LeetCode] Wildcard Matching 通配符匹配

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

[LeetCode]Wildcard Matching 通配符匹配(贪心)

一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,const char *p){ if(flag)return; if(id0>=n){ if(id1>=m)flag=1; else{ int j=0; while(j<m&&p[j]=='*')j++; if(j>=m)flag=1; } return; } else i

LeetCode: Wildcard Matching [043]

[题目] Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The funct

Leetcode: Wildcard Matching. java

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

LeetCode: Wildcard Matching 解题报告

Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial)

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the enti