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 ...
随机推荐
- The Best Rank (25)(排名算法)
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- CodeForces 18C
Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- Xcode常用设置
1.设置主题和字体大小 2.设置显示代码行号
- 基于SuperSocket实现的WebSocket(前端)
本文内容是搭配后端使用的,没看过WebSocket后端实现的童鞋们戳这里 咳咳,其实前端实现相对就容易很多了,因为我们有JavaScript WebSocket Api,它看上来大致是这样的: var ...
- java第一课:环境、变量、数据类型
一.java编程注意事项1.java区分大小写2.每条语句结尾有分号3.上下级代码注意缩进4.大括号要成对出现5.标点符号要用英文半角(半角全角区别)二.eclipse1.eclipse是自编译及时编 ...
- Apache URL rewrite 配置
下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...
- linux cd命令不带路径参数
#切换到当前用户的主目录.若为root用户,则切换到/root,若普通用户,则切换到/home/username $ cd
- MAT使用总结
最近在做项目的时候遇到一个内存泄漏,最后通过MAT定位了问题, 先介绍一下MAT的一些基本概念: Shallow Heap:对象本身占用内存的大小,不包含对其他对象的引用,也就是对象头加成员变量(不是 ...
- HDFS的命令行操作
1.namenode –format:格式化DFS 文件系统 2.secondaryNameNode: 运行DFS的 SecondaryNameNode 进程 hadoop secondaryname ...