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 function prototype should be:
bool isMatch(const char *s, const char *p)
翻译:
" . "匹配任何一个字符。
" * "匹配零个或更多的前面一个元素
匹配应该包括整个输入字符串(不是部分)。
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
我的思路:呵呵哒,没有思路。。。
答案:
public boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) return text.isEmpty();
boolean first_match = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));
if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
return (isMatch(text, pattern.substring(2)) ||
(first_match && isMatch(text.substring(1), pattern)));
} else {
return first_match && isMatch(text.substring(1), pattern.substring(1));
}
}
答案思路:
1、每个元素逐级向后比较,每一级(元素)都会有一个结果,并且都存在分支判断,如果使用循环来做判断太繁琐,所以可以考虑递归;
2、当有多个参数联合判断的时候,首先列表再写判断:(0代表为空)
text pattern
0 0 true
0 1 first = flase 【可能此时有pattern为x*的情况,而text已经被砍为空了,可能结果为匹配,这种情况应该进入后续的判断】
1 0 false
1 1 first = 比较
根据此表写出判空标准。
3、当前比较结果为字母比较或者当前的pattern为“ . ”
4、后续比较分两种情况:
a、此时pattern的前两个为x*,而这种情况的结果为两种结果的或
i、pattern的x*已经和text中的相应字符匹配完毕,需要进行下一个pattern的匹配,于是将pattern的x*去掉
ii、pattern的x*与text的当前字母匹配完毕,需要进行下一个text的匹配,于是将当前结果与text去掉一个的结果做结合【当结果为两种时候使用“||”将两种递归相连】
b、其他情况直接将text与pattern各砍去一个,与当前比较结果结合一起返回。
问:为什么得到当前结果不直接返回?
答:可能此时有pattern为x*的情况,而text已经被砍为空了,可能结果为匹配。只有当pattern为空时不匹配才能直接返回。
LeetCode第[10]题(Java):Regular Expression Matching的更多相关文章
- LeetCode第[44]题(Java):Wildcard Matching
题目:通配符匹配 难度:hard 题目内容: Given an input string (s) and a pattern (p), implement wildcard pattern match ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
随机推荐
- css中的clear:both,display:flex;
介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...
- showslow / YSlow for PhantomJS/slimerjs(gecko)/phantomas
http://yslow.org/phantomjs/ http://www.bstester.com/2015/12/front-end-performance-using-jenkinsphant ...
- XPath 快速入门
XPath 是一门在 xml 文档中查找信息的语言. XPath 使用路径表达式来选取 xml 文档中的节点或者节点集合. 路径表达式由元素和属性组成. 语法介绍 // 示例: // xml 文档 & ...
- Go语言版本的helloworld
新建一个project,然后建立一个main目录,在main目录下新建一个go类文件:main.go 内容如下: package main import "fmt" func ma ...
- 如何实现redis集群?
由于Redis出众的性能,其在众多的移动互联网企业中得到广泛的应用.Redis在3.0版本前只支持单实例模式,虽然现在的服务器内存可以到100GB.200GB的规模,但是单实例模式限制了Redis没法 ...
- 【JUnit】junit4的几个assert方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhtao01/article/details/27858225 在静态类junit.framewor ...
- 【Myeclipse设置】MyEclipse取消Show in Breadcrumb的方法
有时不小心把快捷导航整出来,对于本来就很小的编辑空间来讲就很痛苦了,下面的方法可行,本人亲自试验过. 参考地址:百度文库中的解决方法 在最后用户通过点击出来的图标 ,就可以自如的控制出现和消失了.
- R中apply函数族
参考于:http://blog.fens.me/r-apply/ 1. apply的家族函数 2. apply函数 apply函数是最常用的代替for循环的函数.apply函数可以对矩阵.数据框.数组 ...
- python 2 和python 3的 区别
用户交互 input ps:python2:raw_input python3:input 在 python2里 print不需要加括号也可以打印 子python3里 print 必须加括号才能打印
- Xamrin开发安卓笔记(二)
http://www.cnblogs.com/minCS/p/4112617.html Xamrin开发安卓笔记(二) 安装篇 Xamrin开发安卓笔记(一) 昨天调理一天AAPT.EXE 被推出 ...