题目描述:

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

解题思路:

这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况。以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况。ch*可以匹配0-n个s的字符串,(n是s的起始位置开始值为ch的字符的个数)。当然还要考虑.*……的情况,这样的情况系,就要考虑把.*与s的所有字符都匹配一遍,看能不能找出结果。其他的考虑情况比较容易想到,看下面的代码即可。

具体代码:

  public static boolean isMatch(String s, String p) {
//p为null或者长度为0的情况
if(p==null){
return s==null;
}
if(p.length()==0){
return s.length()==0;
}
if(p.length()==1){
if(s.length()!=1){
return false;
}
else{
if(p.charAt(0)=='.'){
return true;
}
else if(p.charAt(0)=='*'){
return false;
}
else{
return p.charAt(0)==s.charAt(0);
}
}
}
//p至少有长度为2
if(p.contains("*")|| p.contains(".")){
//ch*情况的处理
if(p.charAt(1)=='*'){
char ch = p.charAt(0);
//.*的情况,.*可以匹配s的任意个字符,所以把每种可能的情况递归一遍
if(ch=='.'){
for(int i=0;i<=s.length();i++){
boolean key = isMatch(s.substring(i), p.substring(2));
if(key==true)
return true;
}
}
//ch*的情况,ch*可以匹配0-n个s的字符串,(n是s的起始位置开始值为ch的字符的个数)
else{
int index=0;
while(index<s.length() && s.charAt(index)==p.charAt(0)){
index++;
}
for(int i=0;i<=index;i++){
boolean key = isMatch(s.substring(i), p.substring(2));
if(key==true)
return true;
}
}
}
//不是ch*的情况,即chch……的情况,这时候s的长度要保证大于0
else{
if(p.charAt(0)=='.'){
if(s.length()==0){
return false;
}
boolean key = isMatch(s.substring(1), p.substring(1));
return key;
}
else{
if(s.length()==0){
return false;
}
//如果开头字符相等,匹配与否取决于两字符串除去第一个字符后是否匹配
if(p.charAt(0)==s.charAt(0)){
boolean key = isMatch(s.substring(1), p.substring(1));
return key;
}
else{
return false;
}
}
} return false;
}
//p不包含*,.的情况
else{
if(s.equals(p))
return true;
return false;
}
}

【leetcode】10.Regular Expression Matching的更多相关文章

  1. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

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

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

  3. 【LeetCode】010. Regular Expression Matching

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

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

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

  5. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

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

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

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

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

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

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

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

随机推荐

  1. [Angular 2] Inject Service with "Providers"

    In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...

  2. WWDC2015

     

  3. 【ZZ】一张图清晰追溯数据库的发展历程(1962-2016年)

    http://www.cbdio.com/BigData/2016-02/24/content_4651751.htm 历史发展概述

  4. 文件尾存在EOF吗?

    参考:http://bbs.csdn.net/topics/290027166 我們先一起來看看FILE是怎么定義的:   FILE                          <STDI ...

  5. 如何扩大VMware虚拟机的硬盘磁盘空间大小

    首先,在虚拟机配置界面通过界面配置,直接扩大虚拟机硬盘大小: 而后,登陆虚拟机,在windows磁盘管理,更多操作中直接“重新扫描磁盘”,操作系统自动找到了多出来的磁盘空间: 最后,在老磁盘分区上通过 ...

  6. IP101A芯片默认物理地址(PHY Adress)确定

    转载:http://blog.csdn.net/ropai/article/details/6961157 根据IP101A的DataSheet,芯片的第9,10,12,13,15脚为PHYAD0~P ...

  7. UITableView优化技巧

    UITableView的简单认识 UITableView最核心的思想就是UITableViewCell的重用机制.简单的理解就是:UITableView只会创建一屏幕(或一屏幕多一点)的UITable ...

  8. IIS 发布 之 Word导出本地测试正常,发布报错

    用C#动态生成Word文档功能实现了,在本地的机器运行时是好的,但程序发布 IIS 或 远程服务器 上就报错, 报错信息为:检索 COM 类工厂中 CLSID 为 {000209FF-0000-000 ...

  9. ConcurrentHashMap 源码解析 -- Java 容器

    ConcurrentHashMap的整个结构是一个Segment数组,每个数组由单独的一个锁组成,Segment继承了ReentrantLock. 然后每个Segment中的结构又是类似于HashTa ...

  10. poj3295解题报告(构造、算术表达式运算)

    POJ 3952,题目链接http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为 ...