'.' 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. 【转】在64位windows下使用instsrv.exe和srvany.exe创建windows服务

    本文转自:https://www.iflym.com/index.php/computer-use/201205020001.html 在32位的windows下,包括windows7,windows ...

  2. 让小区运营再智能一点,EasyRadius正式向WayOs用户提供到期弹出式提示充值页面

    其实一直没向用户提供到期弹出式页面,主要是给VIP群的用户一点优越感,随着这次EasyRadius的更新,海哥就免费向普通easyRadius用户提供这两个模板下载. 有些人会问,什么样的模板.有什么 ...

  3. 为什么要用nginx

    1.支持更多的连接数来响应并发请求连接. 2.占用内存小. 3.处理请求的流程和机制决定了他的高并发连接数. 处理并发请求优于apache Web服务器之Nginx详解(理论部分)

  4. Objective-C语法之nonatomic和atomic之间的区别

    atomic: 保证 setter/getter 这两个方法的一个原语操作.如果有多个线程同时调用 setter 的话,不会出现某一个线程执行  setter 全部语句之前,另一个线程开始执行 set ...

  5. 主调度器schedule

    中断处理完毕后,系统有三种执行流向:                                                                               1)直 ...

  6. LR进行接口测试

    其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. 方法一.用Lo ...

  7. 如何在Datatable中取得每列的数据列宽度

    你用SqlDataAdapter填充DataTable的时候不要用Fill方法而应该用FillSchema方法: using (SqlConnection conn = new SqlConnecti ...

  8. sql2008修改管理员与普通用户密码

    方法一: sp_password Null,'123,'sa'把sa的密码设为“123” 执行成功后有“Command(s) completed successfully.” OK! 方法二: 第一步 ...

  9. Centos修改时间显示的时区,将UTC修改为CST

    问题说明: 今天一同事反应,系统的时间不对和正常的时间差8个小时.就登录主机看了下时间 系统时间显示为: # date Fri Dec :: UTC # 备注:查看了下,正好和当前的时间差了8个小时. ...

  10. 字符串中包含汉字和\u,显示出汉字来

    针对py2,py3不需要这样.#coding=utf8import sysreload(sys)sys.setdefaultencoding('utf8') strx2='你好\u4e2d\u56fd ...