一、题目说明

这个题目是10. Regular Expression Matching,乍一看不是很难。

但我实现提交后,总是报错。不得已查看了答案。

二、我的做法

我的实现,最大的问题在于对.*的处理有问题,始终无法成功。

#include<iostream>

using namespace std;

class Solution{
public:
bool isMatch(string s,string p){
bool result = true;
if(s.length()<=0 && p.length()<=0){
return true;
}
if(p==".*"){
return true;
} int sCurr=0,pCurr=0;
int lenS = s.length();
int lenP = p.length(); //count the num of .*
int numOfWildCard = 0;
while(pCurr<lenP){
if(p[pCurr]=='.' && pCurr+1<lenP && p[pCurr+1]=='*'){
numOfWildCard++;
} pCurr++;
}
//cout<<numOfWildCard<<":"; pCurr = 0;
while(sCurr<lenS && pCurr<lenP){
if((pCurr+1<lenP) && p[pCurr]=='.' && p[pCurr+1]=='*'){
if(pCurr+2<lenP){
pCurr = pCurr+2;
while(sCurr<lenS && s[sCurr]!=p[pCurr]){
sCurr++;
}
} }
if((pCurr+1<lenP) && p[pCurr+1]=='*'){
while(sCurr<lenS && s[sCurr]==p[pCurr]){
sCurr++;
}
pCurr = pCurr+2;
}
if(sCurr<lenS && pCurr<lenP && p[pCurr+1]!='*'){
if(s[sCurr]==p[pCurr] || p[pCurr]=='.'){
sCurr++;
pCurr++;
}
}
} if(sCurr==lenS && pCurr==lenP){
result = true;
}else{
result = false;
}
return result;
}
}; int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
return 0;
}

三、正确的做法

1.递归方法

#include<iostream>
#include<vector> using namespace std; class Solution{
public:
bool isMatch(string s, string p) {//aa a
if(p.empty()) return s.empty();
if(s.empty()) return p.empty() || (p[1] == '*' ? isMatch(s, p.substr(2)) : false);
if(p[0] != '.' && s[0] != p[0]) return p[1] == '*' ? isMatch(s, p.substr(2)) : false;
if(p[1] == '*') return isMatch(s.substr(1), p) || isMatch(s, p.substr(2));
return isMatch(s.substr(1), p.substr(1));
}
}; int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
cout<<(false==s.isMatch("ab",".*c"))<<endl;
cout<<(true==s.isMatch("aaa","a*a"))<<endl;
return 0;
}

2.DP方法

dp是什么?动态规划啊,

#include<iostream>
#include<vector>
#include <mem.h> using namespace std; class Solution {
public:
bool isMatch(string s, string p) {
int ssize = s.size(),psize = p.size();
string pp="";
vector<bool> star; for(int i=0;i<p.size();i++){
if(p[i]=='*'){
star.back()=true;
}else{
star.push_back(false);
pp+= p[i];
}
} psize = pp.size(); bool dp[psize+1][ssize+1];
memset(dp,false,sizeof(dp)); dp[0][0] = true;
for(int i=1;i<=psize;i++){
if(star[i-1]==true){
dp[i][0] = true;
}else{
break;
}
} for(int i=1;i<=psize;i++)
for(int j=1;j<=ssize;j++){
if(dp[i-1][j-1]== true){
if(pp[i-1]==s[j-1] || pp[i-1]=='.'){
dp[i][j] = true;
continue;
}
} if(dp[i-1][j]== true){
if(star[i-1]==true){
dp[i][j] = true;
continue;
}
} if(dp[i][j-1]== true){
if(star[i-1]==true && (pp[i-1]==s[j-1] || pp[i-1]=='.')){
dp[i][j] = true;
continue;
}
}
}
return dp[psize][ssize];
}
}; int main(){
Solution s;
cout<<(false==s.isMatch("aa","a"))<<endl;
cout<<(true==s.isMatch("aa","a*"))<<endl;
cout<<(true==s.isMatch("ab",".*"))<<endl;
cout<<(true==s.isMatch("aab","c*a*b"))<<endl;
cout<<(false==s.isMatch("mississippi","mis*is*p*."))<<endl;
cout<<(false==s.isMatch("ab",".*c"))<<endl;
cout<<(true==s.isMatch("aaa","a*a"))<<endl;
cout<<(false==s.isMatch("a",""))<<endl;
return 0;
}

四、总结

看来基础知识还需要恶补,加油!

刷题10. Regular Expression Matching的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  4. 10. Regular Expression Matching字符串.*匹配

    [抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  5. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  6. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  8. 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

随机推荐

  1. Gin_入门

    1. 创建路由 1.1 Restful风格的API gin支持Restful风格的API 即Representational State Transfer的缩写.直接翻译的意思是"表现层状态 ...

  2. 【Python】获取星期字符串

    原理:字符串切片 1.0代码: #获取星期字符串 weekStr="星期一星期二星期三星期四星期五星期六星期日" weekId=eval(input("请输入星期数字(1 ...

  3. Paper: ModelarDB

    Problem: how to store and querry massive amounts of high quality sensor data ingested in real-time f ...

  4. 在MyEclipse中修改文件名出现问题

    问题描述:An exception has been caught while processing the refactoring 'Rename Compilation Unit'. 问题原因:项 ...

  5. linux复制,网络报错

    我拷贝了过来的Linux虚拟机无法上网,我用ifconfig命令查询了一下发现只有eth1和lo设备,没有eth0.于是我在Google上搜索了一下, <VMWare克隆或复制Linux虚拟机后 ...

  6. 将项目部署到linux环境下的Jetty

    1.将项目放到webapps文件夹下 2.进入到jetty/bin目录,有文件jetty.sh 3.运行  命令:./jetty.sh start 4.停止  命令:./jetty.sh stop

  7. Ninject 2.x细说---1.基本使用

    Ninject 2.x细说---1.基本使用 https://blog.csdn.net/weixin_33809981/article/details/86091159   本来想使用一下Ninje ...

  8. array.find()方法

    //array.find(function(currentValue, index, arr),thisValue) 方法说明 let tempArray = [ {"key":1 ...

  9. MySQL数据库、数据表和字段字符集查询、修改和配置

    一.设置编码 LINUX  修改vi/etc/my.cnf WINDOWS my.ini 在[client]下添加    default-character-set=utf8 在[mysqld]下添加 ...

  10. Android开发实战——记账本(3)

    开发日志(3)——适配器 昨天将bean类还有DatabaseHelper类写完.为了在MainActivity中调用,将数据保存到数据库中并显示出来.所以要先编写适配器CostListAdapter ...