刷题10. Regular Expression Matching
一、题目说明
这个题目是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的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
随机推荐
- JUC之CountDownLatch和CyclicBarrier的区别 (转)
CountDownLatch和CyclicBarrier的功能看起来很相似,不易区分,有一种谜之的神秘.本文将通过通俗的例子并结合代码讲解两者的使用方法和区别. CountDownLatch和Cycl ...
- 野路子码农(5)Python中的装饰器,可能是最通俗的解说
装饰器这个名词一听就充满了高级感,而且很多情况下确实也不常用.但装饰器有装饰器的好处,至少了解这个对装逼还是颇有益处的.网上有很多关于装饰器的解说,但通常都太过“循序渐进”,有的还会讲一些“闭包”之类 ...
- Token:服务端身份验证的流行方案
01- 身份认证 服务端提供资源给客户端,但是某些资源是有条件的.所以服务端要能够识别请求者的身份,然后再判断所请求的资源是否可以给请求者. token是一种身份验证的机制,初始时用户提交账号数据给服 ...
- 【网页浏览】关键字搜索PIXIV插图
可以在线搜索Pixiv(P站)的插图 传送链接
- Pytest学习6-跳过或xfail失败的用例
Skip跳过用例 跳过(Skip)指,你希望如果某些条件得到满足你的测试用例才执行,否则Pytest应该完全跳过运行该用例 1. 跳过测试用例的最简单方法是使用skip装饰器标记它,可以传递一个可选的 ...
- 记录 shell学习过程(2) read的用法
echo -n "login:"read username #read后面直接使用一个变量用于接收输入的数据 echo -n "password:"read ...
- C++——简单程序设计
1.一个简单的程序 #include <iostream> //iostream是头文件,用来说明要使用的对象的相关信息. using namespace std; //使用命名空间,解决 ...
- js-时间相关的转换
毫秒值 -> 时间 var date = new Date(1477386005*1000);
- go 语言实现栈原理
package main import "fmt" type StackNode struct { Data interface{} //数据 Next *StackNode // ...
- jvm(1):内存结构
JVM内存结构 JVM内存的运行时数据区: 线程私有(在线程启动时创建) 程序计数器Program Counter Register 一块较小的内存空间,可以看作是当前线程所执行的字节码的行号指示器, ...