一、题目说明

这个题目是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. 支持CSS3的高级CSS

    一.匹配部分字符串 在CSS2中,可以使用[ href='info.htm' ] 这样的属性选择器来匹配href属性中的字符,但是无法只匹配部分字符串.而CSS3给出了三个新的运算符:^ , $ 和 ...

  2. HTML下拉请求列表标签

    select下拉请求列表 option选择项(内容) selected默认值

  3. python:运行command

    #!/usr/bin/python# -*- coding:utf-8 -*- import os os.system('cocos jscompile -s ./dir1 -d ./dir2')

  4. HITCON-Training-Writeup

    HITCON-Training-Writeup 原文链接M4x@10.0.0.55 项目地址M4x's github,欢迎star~ 更新时间5月16 复习一下二进制基础,写写HITCON-Train ...

  5. 208. 实现 Trie (前缀树)

    主要是记录一下这个数据结构. 比如这个trie树,包含三个单词:sea,sells,she. 代码: class Trie { bool isWord; vector<Trie*> chi ...

  6. 创建pod失败分析

  7. Java连载77-Integer常用方法、Integer、int、String三者相互转化、自动装箱、自动拆箱

    一.关于Integer中常用的方法 package com.bjpowernode.java_learning; ​ public class D77_1_ { public static void ...

  8. 计算机网络,HTTP - 头部中带X前缀的头部字段

    HTTP中,什么是"X-" Prefix header? 例如 github API 的response headers有很多X前缀的头部: 查一下MDN文档: Custom pr ...

  9. thows,thow和try catch的区别

    1.throw是当前方法不处理这个异常,由它的上一级进行处理.并且抛出异常后将停止执行代码. package myProject; public class ExceptionTest { //测试t ...

  10. spring(三):BeanFactory