Java [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.
- http://i.cnblogs.com/EditPosts.aspx?opt=1
- 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
解题思路:
从字符串s和p的位置sp,pp处分别开始匹配,运用递归的方法不断缩小字符串的比较长度,最后回溯回来比较结果。
假设现在走到分别走到sp,pp处。则分为下面几种情况:
(1)如果pp的位置已经越界,那么若sp也已越界,那么则返回true;否则说明s字符串还有长度,不匹配,返回false;
(2)pp的位置为p的最后一位,那么此时只能一一匹配,若不匹配则返回false;否则进行下一位比较;
(3)pp的位置比较靠前,那么比较pp的下一位。如果下一位不是'*',则只能一一匹配,若不匹配则返回false;否则进行下一位比较。如果下一位是'*',则将pp的位置向后移动两位,sp的位置从现在开始进行暴力比较,每次向后移一位进行递归比较。
代码如下:
- public class Solution {
- public static boolean isMatch(String s, String p) {
- if (s == null)
- return p == null;
- if (p == null)
- return s == null;
- return helper(s, p, 0, 0);
- }
- private static boolean helper(String s, String p, int sp, int pp) {
- if (pp >= p.length())
- return sp >= s.length();
- // pp comes to end
- if (pp == p.length() - 1) {
- if (sp >= s.length()
- || (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
- return false;
- else
- return helper(s, p, sp + 1, pp + 1);
- }
- // p.charAt(pp+1)!='*'
- if (p.charAt(pp + 1) != '*') {
- if (sp >= s.length()
- || (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
- return false;
- else
- return helper(s, p, sp + 1, pp + 1);
- }
- // p.charAt(pp+1)=='*'
- while (sp < s.length()
- && (p.charAt(pp) == '.' || s.charAt(sp) == p.charAt(pp))) {
- if (helper(s, p, sp, pp + 2))
- return true;
- sp++;
- }
- return helper(s, p, sp, pp + 2);
- }
- }
Java [leetcode 10] Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 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 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [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 [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- [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 ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- jQuery 的 json 格式的处理问题
在实际的使用中时常会发生一些ajax验证的诡异事件,如我在一个文件中使用json传送数据,结果出现了当数据发送到服务器端时(后端主要呈现的是对json数据对象的数据库查询操作),结果显示的是数据已经插 ...
- Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Unity3d 如何找到游戏对象并改变其颜色
//游戏对象 private var obj:GameObject; //渲染器 private var render:Renderer; //贴图 private var texture:Textu ...
- EntityFramework.Extended
记录 Entity Framework扩展,可以实现批量更新.删除,但需要EntityFramework6.0支持,需要支持低版本的EF,可下载该扩展的低版本. https://www.nuget.o ...
- Transaction Log Truncation
--method 1-- ALTER DATABASE KIS_Sample3 SET RECOVERY SIMPLE ) ALTER DATABASE KIS_Sample3 SET RECOVER ...
- 去掉url后面的#
<a href="#" 这个代码,当你点击后会在url后面添加# 怎么去掉呢?解决方法:点击这里 <a href="javascript:void(0)&qu ...
- mysql 增加删除用户
mysql 增加用户 (注意:因为MYSQL环境中的命令,所以后面都带一个分号作为命令结束符) 格式:grant select on 数据库.* to 用户名@登录主机 identified by ' ...
- MVC中的@Html.DisplayFor等方法如何控制日期的显示格式(转) - Rising
在Sql Server2005中,如果将某字段定义成日期 时间 类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年 ...
- Unity3D开发(一):NGUI之UIRoot屏幕分辨率自适应
原地址:http://blog.csdn.net/onerain88/article/details/11713299 NGUI在Unity3D游戏开发中非常常用,而NGUI对于每一个UI场景,都是以 ...
- APP 上传之后出现"invalid binary" 问题解决汇总
背景 5.1 号开始 App 审核开始强制支持 iPhone5,并禁止使用 UDID. 问题 上传 app 后一直处于 Invalid Binary 状态,并且收到一封邮件说 Non-public A ...