Problem 10:Regular Expression Matching

来源:互联网 发布:软件外包群 编辑:程序博客网 时间:2024/06/05 09:11

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

'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
题解:
本文及代码引用自:http://www.cnblogs.com/springfor/p/3893593.html?utm_source=tuicool
  • 首先要理解题意:
    • "a"对应"a", 这种匹配不解释了
    • 任意字母对应".", 这也是正则常见
    • 0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是 相同的字符中取一个,比如"aaaab"对应是"a*b"
    • "*"还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"a*a", 代码逻辑不能一路贪婪到底,所以在p.charAt(1)=='*'条件下,while在条件成立的情况下,s不能无限递减,需要判断isMatch(s,p.substring(2))是否成立,如果成立则返回true,最好的例子就是s="aaa",p="a*a"这个测试用例。
  • 正则表达式如果期望着一个字符一个字符的匹配,是非常不现实的.而"匹配"这个问题,非 常容易转换成"匹配了一部分",整个匹配不匹配,要看"剩下的匹配"情况.这就很好的把 一个大的问题转换成了规模较小的问题:递归
  • 确定了递归以后,使用java来实现这个问题,会遇到很多和c不一样的地方,因为java对字符 的控制不像c语言指针那么灵活charAt一定要确定某个位置存在才可以使用.
  • 如果pattern是"x*"类型的话,那么pattern每次要两个两个的减少.否则,就是一个一个 的减少. 无论怎样减少,都要保证pattern有那么多个.比如s.substring(n), 其中n 最大也就是s.length()

solution代码:
/** * Created by liuyang on 2015/8/30. */public class Solution {    public static boolean isMatch(String s, String p) {        if (p.length() == 0)            return s.length() == 0;        // length == 1 is the case that is easy to forget.        // as p is subtracted 2 each time, so if original        // p is odd, then finally it will face the length 1        if (p.length() == 1){            return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');        }        // next char is not '*': must match current character        if (p.charAt(1) != '*') {            if (s.length() == 0)                return false;            else                return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')                        && isMatch(s.substring(1), p.substring(1));        }else{            // next char is *            while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {                if (isMatch(s, p.substring(2))){                    return true;                }                s = s.substring(1);            }            return isMatch(s, p.substring(2));        }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子吃饭不专心怎么办 上课不专心听怎么办 做作业总是走神怎么办 做事注意力不集中怎么办 做事注意力不能集中怎么办 幼儿争抢玩具老师怎么办 孩子不专心学习怎么办 一年级孩子学习边学边忘怎么办 对爱不专心怎么办 孩子脑子不灵活怎么办 小孩写作业心慌怎么办 4岁写数字怎么办 孩子做事太慢怎么办 小孩子做事不认真怎么办 员工做事不认真怎么办 做事总是不认真怎么办 孩子上课老是讲话怎么办 孩子上课总讲话怎么办 孩子不求上进怎么办 孩子只知道吃完怎么办 孩子演出前紧张怎么办 儿童写字不专心怎么办 孩子上课没精神怎么办 小孩写字太慢怎么办 儿童上课不集中怎么办 突然头晕迷糊眼花怎么办 自己上课不专心怎么办 颈椎病头晕怎么办才好 孩孑记忆力不好怎么办 上课的时候困怎么办 小学生写字太慢怎么办 一年级小学生写字慢怎么办 小学生写字太用力怎么办 专注力不集中怎么办 一年级孩子写字慢怎么办 孩子话很多应该怎么办 孩子废话特别多怎么办 老师不认真教学怎么办 小孩脸上长痦子怎么办 小朋友爱讲笑话怎么办 小孩上课不认真怎么办