'.' Matches any single character.匹配任何单字符
'*' Matches zero or more of the preceding element.匹配0个或者多个前置元素

采用动态规划方法

public boolean isMatch(String s, String p)
1, If p.charAt(j) == s.charAt(i) :  dp[i][j] = dp[i-1][j-1];
进行下一层的计算
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
字符为’.'时也进行下一层dp[i-1][i-1]运算
3, If p.charAt(j) == '*’: 
当字符为’*’时,需要进行分类考虑
   here are two sub conditions:

   1 if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] //in this case, a* only counts as empty
当’*’前面的一个字符和匹配串字符不相同时,则从模式串删去*以及其前面一个字符

   2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.’:

dp[i][j] = dp[i-1][j] //in this case, a* counts as multiple a

or dp[i][j] = dp[i][j-1] // in this case, a* counts as single a

or dp[i][j] = dp[i][j-2] // in this case, a* counts as empty 参考代码:
package leetcode;

/***
*
* @author pengfei_zheng
* 字符串匹配问题
*/
public class Solution10{
public boolean isMatch(String s, String p) {
return match(s,p,0,0);
}
private boolean match(String s,String p,int i,int j){//其中i,j分别为开始下标
if(j==p.length())//匹配至长度相同
return i==s.length();
if(j==p.length()-1 || p.charAt(j+1)!='*'){//匹配至下一个字符不为'*'
if(i==s.length() || s.charAt(i)!=p.charAt(j) && p.charAt(j)!='.')//不相等或者不等于'.'
return false;
else
return match(s,p,i+1,j+1);
}
while(i<s.length() && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='.')){//相等或者等于'.'
if(match(s,p,i,j+2))
return true;
i++;
}
return match(s,p,i,j+2);
}
}

 

LeetCode 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. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

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

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

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

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

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

  7. [leetcode]10. Regular Expression Matching正则表达式的匹配

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

  8. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

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

  9. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  10. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. JAVA的get post 区别

    1. get 是从服务器上获取数据,post 是向服务器传送数据. get 请求返回 request - URI 所指出的任意信息.Post 请求用来发送电子邮件.新闻或发送能由交互用户填写的表格.这 ...

  2. 查看已装TensorFlow的版本和路径

    pyhton Python 2.7.14... >>>import tensorflow as tf >>>tf.__version__ '1.3.0'>&g ...

  3. 原理分析之一:从JDBC到Mybatis

    原理分析之一:从JDBC到Mybatis Mybatis学习(一)原生态的JDBC编程总结 -----系列 深入浅出MyBatis-快速入门

  4. OpenGL ES学习资料总结

    从今年春节后开始学习OpenGL ES,发现网上资料很有限,而且良莠不齐,所以整理了一下我学习时用到的资料和一些心得. 1. OpenGL ES1.x参考资料 把NEHE的教程移植到了Android上 ...

  5. LoadRunner做性能测试 从设计到分析执行

    项目简介:像百度知道系统类似的系统性能测试,是公司的自己产品. 对最近这个系统的性能测试进行总结下: 系统功能介绍: 前台用户可以根据自己的需要对不同的区域提问,提问包括匿名和登陆用户提问 后台不同区 ...

  6. 网络上可供测试的Web Service

    网络上可供测试的Web Service 腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService ...

  7. 修改linux终端DIR显示颜色

    头疼死,linux终端下,目录颜色蓝色在黑色的背景下,睁大双眼都看不清楚. 找办法修改,找到默认的颜色设置目录: # vi /etc/DIR_COLORS 查看文件,并查找DIR: 可以看到默认设定“ ...

  8. Linux Shell 通配符、元字符、转义符使用实例介绍

    https://www.cnblogs.com/chengmo/archive/2010/10/17/1853344.html

  9. ASP.NET Web API 使用Swagger使用笔记

    https://www.cnblogs.com/lhbshg/p/8711604.html 最近换了工作,其中Webapi这块没有文档,之前有了解过Swagger借此机会好好整理下常用的地方分享给有需 ...

  10. jdbc数据访问技术

    jdbc数据访问技术 1.JDBC如何做事务处理? Con.setAutoCommit(false) Con.commit(); Con.rollback(); 2.写出几个在Jdbc中常用的接口 p ...