天题系列:Regular Expression Matching

烧脑神题

public class Solution {
    public boolean isMatch(String s, String p) {
        // 反正我是想不出来 http://www.cnblogs.com/springfor/p/3893593.html
        if(p.length()==0) return s.length()==0;
        if(p.length()==1) return (s.length()==1 &&(p.charAt(0)==s.charAt(0)||p.charAt(0)==‘.‘));

        if(p.charAt(1)!=‘*‘){
            if(s.length()<1) return false;   

            return (s.charAt(0)==p.charAt(0)||p.charAt(0)==‘.‘)&&(isMatch(s.substring(1), p.substring(1)));
        }else{
            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); // p= a*????; s = aaaaa????
            }
            return isMatch(s, p.substring(2)); // p= b*?????, s = a????
        }

    }
}
时间: 2024-10-13 20:52:08

天题系列:Regular Expression Matching的相关文章

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

leetcode第十题--Regular Expression Matching

Problem: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 s

【leetcode刷题笔记】Regular Expression Matching

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

LeetCode第[10]题(Java):Regular Expression Matching

题目:匹配正则表达式 题目难度:hard 题目内容: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 fu

66. Regular Expression Matching

Regular Expression Matching 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

Regular Expression Matching &amp; Wildcard Matching

Regular Expression Matching 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

[LeetCode] Regular Expression Matching 正则表达式匹配

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

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/regular-expression-matching/ [描述] 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 ent

leetcode——Regular Expression Matching

题目: 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 shoul

regular expression matching DP

这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: 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 en