Leetcode 10.正则表达式匹配 动态规划
请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
leetcode原题
Regular Expression Matching
思路:只有当p==\0时,判断s==\0;否则会出现:s=\0,p=c*。
先判断是否p+1==*,然后进入循环,循环结束return match(str,p+2),不然会出现s=\0,p=.*,进入不了循环的状况。
else if 时要加上str!=\0,不然会出现s=\0,p=.的状况,都+1后s=null。
1 class Solution { 2 public: 3 bool match(char* str, char* pattern) 4 { 5 if(*pattern=='\0') 6 return *str=='\0'; 7 if(*(pattern+1)=='*'){ 8 while(*str!='\0'){ 9 if(match(str,pattern+2)) 10 return true; 11 if(*str==*pattern||*pattern=='.') 12 str++; 13 else 14 return false; 15 } 16 return match(str,pattern+2); 17 } 18 else if(*pattern==*str||(*pattern=='.'&&*str!='\0')){ 19 return match(str+1,pattern+1); 20 } 21 return false; 22 } 23 };
思路二:
注意首先判断j==p.size()时,return i==s.size
然后判断p[j+1]=='*'的情况
class Solution { public: bool isMatch(string s, string p) { int m=s.size(); int n=p.size(); return match(s,0,p,0); } bool match(string s,int i,string p,int j){ if(j==p.size()) return i==s.size(); if(j+1<p.size()&&p[j+1]=='*'){ while(i<s.size()){ if(match(s,i,p,j+2)) return true; if(s[i]==p[j]||p[j]=='.') i++; else return false; } return match(s,i,p,j+2); } else if(i<s.size()&&(s[i]==p[j]||p[j]=='.')) return match(s,i+1,p,j+1); return false; } };
思路三:
dp数组实现
定义dp[i][j]表示i长度的s与j长度的p是否匹配
则dp[0][0]=true
dp[i][j] = dp[i - 1][j - 1]
, ifp[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.')
;dp[i][j] = dp[i][j - 2]
, ifp[j - 1] == '*'
and the pattern repeats for 0 time;dp[i][j] = dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')
, ifp[j - 1] == '*'
and the pattern repeats for at least 1 time.
注意,p[j-1]=='*'时,dp[i][j]=dp[i-1][j]&&...
class Solution { public: bool isMatch(string s, string p) { int m=s.size(); int n=p.size(); vector<vector<bool>> dp(m+1,vector<bool>(n+1)); //s和p都为0,肯定匹配 dp[0][0]=true; //注意i从0开始,因为s=0,p=a* 或者a*b*的时候,dp[0][2]=true dp[0][4]=true for(int i=0;i<=m;++i){ for(int j=1;j<=n;++j){ //p[j-1]==* if(p[j-1]=='*'){ //直接略过j-2和j-1 || i>0时 s==p或者p=='.' dp[i][j]=dp[i][j-2]||(i&&dp[i-1][j]&&(s[i-1]==p[j-2]||p[j-2]=='.')); }else{ dp[i][j]=i&&dp[i-1][j-1]&&(s[i-1]==p[j-1]||p[j-1]=='.'); } } } return dp[m][n]; } };
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库