题目描述:

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. C#.net 之货币转换

    利用string.format 和cultureInfo 来进行转换 /// <summary> /// 输入Float格式数字,将其转换为货币表达方式 /// </summary& ...

  2. 【Lucene4.8教程之四】分析

    1.基础内容 (1)相关概念 分析(Analysis),在Lucene中指的是将域(Field)文本转换成最主要的索引表示单元--项(Term)的过程.在搜索过程中,这些项用于决定什么样的文档可以匹配 ...

  3. iOS6 / iOS7 状态栏高度适配

    问题原因:iOS7的状态栏(status bar)不再占用单独的20px,所以假设你在iOS6上的界面布局是正常的,那么到了iOS7上就会变成以下这个样子:             左边是iOS6界面 ...

  4. 解决Mac下SublimeLinter的Unsafe Characters警告

    Mac下编辑JS文件, 如果是中文字符的行会警告: This character may get silently deleted by one or more browsers. SublimeLi ...

  5. mybatis0212 mybatis逆向工程 (MyBatis Generator)

    1mybatis逆向工程 (MyBatis Generator) .1什么是mybatis的逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括生成 :mapper.xml.m ...

  6. Playing with ptrace, Part I

    X86_64 的 Redhat / Centos / Scientific 下面,若要编译.运行32位程序,需要安装以下包: yum install libgcc.i686 yum install g ...

  7. placement new 操作符

    placement new操作符能够在分配内存时指定内存位置.下面的程序使用了placement new操作符和常规new操作符给对象分配内存. // placenew.cpp -- new, pla ...

  8. 学习SDAutoLayout第三方库的用法总结

    ---恢复内容开始--- 在做图文混排计算高度我觉得SDAutoLayout布局是最好用的第三方库了,这里学习SDAutoLayout用法总结一下 用法介绍: ) 方法名中带有“SpaceToView ...

  9. C++编译器的函数名修饰规则

    我们知道在C++中有函数重载这样一个东西,当我们定义了几个功能类似且函数名是一样的函数的时候,只要它的参数列表不同,编译是可以通过的,但是在C中是不可以的. double add(double a, ...

  10. ubuntu14_pip 安装

    1:install pip python-dev    sudo apt-get install python-dev    sudo apt-get install libevent-dev     ...