标签:col click color 图片 opened car turn solution bsp
这道题跟leetcode的第10道题差不多;都是用差不多相同的思想解题。
当为?或者p1 == s1 的时候,则dp[p1][s1] = dp[p0][s0]
当为*的时候,则dp[p1][s1] = dp[p1][s0] | dp[p0][s0] | dp[p0][s1]; 因为,此时p1可以等于s1,则为dp[p0][s0], 也可以p1不存在,则为dp[p0][s1]。 也可以为p2等于s1,(因为*可以有n个任意字符)则为dp[p2][s1],又因为dp[p2][s1] == dp[p1][s0];
class Solution { public: bool isMatch(string s, string p) { vector<int> vec(s.length() + 1, 0); vec[0] = 1; for (int i = 0; i < p.length(); ++i) { if (p[i] == ‘*‘) { for (int j = 0; j < s.length(); ++j) vec[j+1] |= vec[j]; } else { for (int j = s.length() - 1; j >= 0; --j) { if (p[i] == s[j] || p[i] == ‘?‘) vec[j+1] = vec[j]; else vec[j+1] = 0; } vec[0] = 0; } } return vec[s.length()]; } };
如下反向遍历,则无需记录当前的状态
for (int j = s.length() - 1; j >= 0; --j) { if (p[i] == s[j] || p[i] == ‘?‘) vec[j+1] = vec[j]; else vec[j+1] = 0; }
leetcode 44. Wildcard Matching
标签:col click color 图片 opened car turn solution bsp
原文地址:https://www.cnblogs.com/czwlinux/p/12364347.html