第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

Leetcode 44

实现一种类似正则表达式的字符串匹配功能。

复杂度要求不高, 调代码稍微费点劲。。

好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧。。

class Solution {
public:
    bool match(string &s,string &p,int l2,int r2,int l1)
{
 if(l2==r2)return true;
 if(l1+r2-l2-1>=s.length())return false;
 while(l2<r2)
    {
	if(p[l2]==‘?‘){l1++;l2++;continue;}
 	if(s[l1]!=p[l2])return false;
 	l1++;l2++;
	}
 return true;
}
bool isMatch(string s, string p) {
	s.append(1,‘#‘);p.append(1,‘#‘);
	int l1=0,l2=0,r1=0,r2=0,len1=s.length(),len2=p.length();
    while(true)
    	{
    	 while(r2<len2&&p[r2]!=‘*‘)
    	 	 r2++;
		 while(!match(s,p,l2,r2,l1))
		 	{
		 	 if(l1>=len1)return false;
		 	 if(l2>0&&p[l2-1]==‘*‘)l1++;
		 	 	else return false;
			}
		 l1+=r2-l2;
		 l2=r2+1;
		 r2=l2;
		 if(l1>=len1&&r2>=len2)return true;
		 if(r2>=len2)return false;
		}

    }
};

  

时间: 2024-10-10 21:06:06

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)的相关文章

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,

(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

19.2.4 [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

LeetCode 44 Wildcard Matching(字符串匹配问题)

题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 字符串匹配问题. 如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串 给定字符串s,和字符串p.判

[动态规划] leetcode 44 Wildcard Matching

problem:https://leetcode.com/problems/wildcard-matching/ 用记忆化搜索做的,不容易出错: class Solution { public: vector<vector<int>> dp; bool isMatch(const string& s, const string& p, int i, int j) { if(i >= s.size() && j >= p.size()) r

44. Wildcard Matching(js)

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

【leetcode】Wildcard Matching

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 partia

[LeetCode][JavaScript]Wildcard Matching

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 partia

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]