【leetcode】10.Regular Expression Matching

题目描述:

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的所有字符都匹配一遍,看能不能找出结果。其他的考虑情况比较容易想到,看下面的代码即可。

具体代码:

 1  public static boolean isMatch(String s, String p) {
 2          //p为null或者长度为0的情况
 3          if(p==null){
 4             return s==null;
 5          }
 6          if(p.length()==0){
 7             return s.length()==0;
 8          }
 9          if(p.length()==1){
10              if(s.length()!=1){
11                  return false;
12              }
13              else{
14                  if(p.charAt(0)=='.'){
15                      return true;
16                  }
17                  else if(p.charAt(0)=='*'){
18                      return false;
19                  }
20                  else{
21                      return p.charAt(0)==s.charAt(0);
22                  }
23              }
24          }
25          //p至少有长度为2
26          if(p.contains("*")|| p.contains(".")){
27              //ch*情况的处理
28              if(p.charAt(1)=='*'){
29                  char ch = p.charAt(0);
30                  //.*的情况,.*可以匹配s的任意个字符,所以把每种可能的情况递归一遍
31                  if(ch=='.'){
32                      for(int i=0;i<=s.length();i++){
33                          boolean key = isMatch(s.substring(i), p.substring(2));
34                          if(key==true)
35                              return true;
36                          }
37                      } 
38                  //ch*的情况,ch*可以匹配0-n个s的字符串,(n是s的起始位置开始值为ch的字符的个数)
39                  else{
40                      int index=0;
41                      while(index<s.length() && s.charAt(index)==p.charAt(0)){
42                          index++;
43                      }
44                      for(int i=0;i<=index;i++){
45                          boolean key = isMatch(s.substring(i), p.substring(2));
46                          if(key==true)
47                             return true;
48                      }
49                 }
50             }
51             //不是ch*的情况,即chch……的情况,这时候s的长度要保证大于0
52             else{
53                 if(p.charAt(0)=='.'){
54                     if(s.length()==0){
55                         return false;
56                     }
57                     boolean key = isMatch(s.substring(1), p.substring(1));
58                     return key;
59                 }
60                 else{
61                     if(s.length()==0){
62                         return false;
63                     }
64                     //如果开头字符相等,匹配与否取决于两字符串除去第一个字符后是否匹配
65                     if(p.charAt(0)==s.charAt(0)){
66                         boolean key = isMatch(s.substring(1), p.substring(1));
67                             return key;
68                         }
69                     else{
70                         return false;
71                     }
72                 }
73             }
74                 
75             return false;
76         }
77          //p不包含*,.的情况
78         else{
79            if(s.equals(p))
80                return true;
81           return false;
82        }
83    }

 

转载于:https://www.cnblogs.com/godlei/p/5582603.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值