题目描述:

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. Codeforces Testing Round #12 B. Restaurant 贪心

    B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...

  2. C++ Button右键弹出式菜单

    Button右键弹出式菜单 关键点 用类来实现 的 实现过程 新建1个类  类名CButtonPopMenu 基类CButton 新建1个菜单资源 IDR_MENU1 // ButtonPopMenu ...

  3. 模拟TAB键

    模拟TAB键 (2013/6/7 22:35:29) SelectNext(ActiveControl,True,True); 屏蔽Alt+F4关闭键 (2013/6/7 22:35:39) 启动某些 ...

  4. 实时数据采集传输软件LDM配置

    本环境一共两个机器:cma.ldm87.gov.cn(hostname)机器作为upstream LDM,cma.ldm84.gov.cn(hostname)机器作为downstream LDM.下面 ...

  5. mac 功能修改。。。。

    个人表示 Mac 下的 Spotlight 搜索功能确实是个鸡肋,安装 QuickSilver 才是王道!所以我个人就把 Spotlight 关闭掉了.方法很简单,还是要用到 “终端” 工具. 在 “ ...

  6. mysql 5.6 binlog组提交

    mysql 5.6 binlog组提交实现原理 http://blog.itpub.net/15480802/viewspace-1411356 Redo组提交 Redo提交流程大致如下 lock l ...

  7. 进程控制之system函数

    ISO C定义了system函数,但是其操作对系统的依赖性很强.POSIX.1包括了system接口,它扩展了ISO C定义,以描述system在POSIX.1环境中的运行行为. #include & ...

  8. 申请TexturePacker 或 PhysicsEditor free licenses

    有一个跟开发有关的blog,就可以去 http://www.codeandweb.com/request-free-license 申请一个free licenses. 可以申请TexturePack ...

  9. 5个可以帮你优化App的优秀网站

    也许现在有一款App可以提供所有你需要的,你不需要的,或者你可以想象到的内容.但是,有多少App真的可以不仅满足需求而且还能提供很好的用户体验呢? 相信很多APP并没有这样的能力.有一些APP的设计特 ...

  10. 一个开源音乐播放器,低仿QQ音乐!

    有暇,弄了个音乐播放器,页面效果整体上参考了QQ音乐,相关API使用了易源数据提供的相关接口(https://www.showapi.com/api/lookPoint/213),在此表示感谢.先来看 ...