bobo老师,第十题的思路我和某个发布题解的作者一样(下面是题解的链接),但是我还有一点歧义
![
就是我用红色框框圈起来的这个,我觉得当p中的下一个位置不是*时,与当前s的位置不匹配,应该查询p中的下一个位置与当前s的位置是否匹配,而不是直接返回false
http://img1.sycdn.imooc.com//szimg/5f32907509d8321a09430624.jpg)
https://leetcode-cn.com/problems/regular-expression-matching/solution/c-4ms-zhi-guan-fen-xi-by-fu-guang/
这是我的代码:
class Solution {
private:
bool doMatch(const string& s, int sIndex, const string& p, int pIndex) {
if (pIndex >= p.size()) return sIndex >= s.size();
bool curElement = sIndex<s.size()&&(s[sIndex] == p[pIndex] || (p[pIndex] == '.'));
if (pIndex + 1 < p.size() && p[pIndex+1] == '*') {
return doMatch(s, sIndex, p, pIndex + 2) || curElement&&doMatch(s, sIndex + 1, p, pIndex);
}
else {
if(curElement==true){
return doMatch(s, sIndex + 1, p, pIndex + 1);
}
else{
return doMatch(s,sIndex,p,pIndex+1);
}
}
}
public:
bool isMatch(string s, string p) {
return doMatch(s, 0, p, 0);
}
};