leetcode 44. Wildcard Matching

Posted 晴朗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 44. Wildcard Matching相关的知识,希望对你有一定的参考价值。

动态规划:

1:状态转移方程

if(‘?‘ == p[j - 1] || s[i - 1] == p[j - 1])
vvb[i][j] = vvb[i - 1][j - 1];

 if(‘*‘ == p[j - 1])
vvb[i][j] = vvb[i][j - 1] || vvb[i - 1][j - 1] || vvb[i-1][j];

 

边界情况主要考虑两种:

1:s="abadfs",p="*"

2.s="",p="*"

 

class Solution
{
public:
bool isMatch(string s, string p)
{
int ls = s.size(), lp = p.size();
vector<vector<bool> > vvb(ls + 1, vector<bool>(lp + 1, false));
vvb[0][0] = true;

for(int j = 1; j <= lp; ++ j)
{
vvb[0][j] = vvb[0][j - 1] && ‘*‘ == p[j - 1];
for(int i = 1; i <= ls; ++ i)
{
vvb[i][0]=vvb[i-1][0] && ‘*‘ == p[0];
if(‘?‘ == p[j - 1] || s[i - 1] == p[j - 1])
vvb[i][j] = vvb[i - 1][j - 1];
else if(‘*‘ == p[j - 1])
vvb[i][j] = vvb[i][j - 1] || vvb[i - 1][j - 1] || vvb[i-1][j];
// cout<<i<<" "<<j<<" "<<vvb[i][j]<<endl;
}
}

return vvb[ls][lp];
}
};




























以上是关于leetcode 44. Wildcard Matching的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 44: Wildcard Matching

LeetCode-44-Wildcard Matching

LeetCode44 Wildcard Matching

leetcode-44. Wildcard Matching

[动态规划] leetcode 44 Wildcard Matching

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