剑指offer-正则表达式匹配
来源:互联网 发布:怎么配置linux ip 编辑:程序博客网 时间:2024/05/01 07:53
正则表达式匹配
题目描述
请实现一个函数用来匹配包括’.’和’‘的正则表达式。模式中的字符’.’表示任意一个字符,而’‘表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配,特别的注意,a* 、a*b* 等模式是可以匹配0一个字符串的
问题分析
下面是匹配结束情形:
str到尾,pattern到尾,匹配成功
if (strIndex == str.length && patternIndex == pattern.length) { return true; }
str未到尾,pattern到尾,匹配失败
if (strIndex != str.length && patternIndex == pattern.length) { return false;}
str到尾,pattern未到尾(不一定匹配失败,因为a*可以匹配0个字符)
if (strIndex == str.length && patternIndex != pattern.length) { //只有pattern剩下的部分类似a*b*c*的形式,才匹配成功 // 递归匹配 if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') { return matchCore(str, strIndex, pattern, patternIndex + 2); } return false;}
str未到尾,pattern未到尾,且模式串后一个字符为*,
if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') { if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) { return matchCore(str, strIndex, pattern, patternIndex + 2)//*匹配0个,跳过 || matchCore(str, strIndex + 1, pattern, patternIndex + 2)//*匹配1个,跳过 || matchCore(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个 } else { //直接跳过*(*匹配到0个) return matchCore(str, strIndex, pattern, patternIndex + 2); } }
当前匹配成功,但是模式下一个元素不是*
if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) { return matchCore(str, strIndex + 1, pattern, patternIndex + 1); }
其他匹配失败,返回失败
return false;
由于涉及子串的比较,递归无疑是最好的办法,完整代码为:
package com.genge.offer;/** * Created by Genge on 2016-07-07. * * 正则表达式匹配 */public class Match { /** * 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符, * 而'*'表示它前面的字符可以出现任意次(包含0次)。 * 在本题中,匹配是指字符串的所有字符匹配整个模式。 * 例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 * @param str * @param pattern * @return */ public boolean match(char[] str, char[] pattern) { if (str == null || pattern == null) { return false; } int strIndex = 0; int patternIndex = 0; return matchCore(str, strIndex, pattern, patternIndex); } public boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) { //str到尾,pattern到尾,匹配成功 if (strIndex == str.length && patternIndex == pattern.length) { return true; } //str未到尾,pattern到尾,匹配失败 if (strIndex != str.length && patternIndex == pattern.length) { return false; } //str到尾,pattern未到尾(不一定匹配失败,因为a*可以匹配0个字符) if (strIndex == str.length && patternIndex != pattern.length) { //只有pattern剩下的部分类似a*b*c*的形式,才匹配成功 if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') { return matchCore(str, strIndex, pattern, patternIndex + 2); } return false; } //str未到尾,pattern未到尾 if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') { if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) { return matchCore(str, strIndex, pattern, patternIndex + 2)//*匹配0个,跳过 || matchCore(str, strIndex + 1, pattern, patternIndex + 2)//*匹配1个,跳过 || matchCore(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个 } else { //直接跳过*(*匹配到0个) return matchCore(str, strIndex, pattern, patternIndex + 2); } } if (pattern[patternIndex] == str[strIndex] || (pattern[patternIndex] == '.' && strIndex != str.length)) { return matchCore(str, strIndex + 1, pattern, patternIndex + 1); } return false; }}
此题要求逻辑清晰,对各种情形进行分类处理,不多也不能少
1 0
- 《剑指offer》正则表达式匹配
- 剑指offer--正则表达式匹配
- 剑指offer:正则表达式匹配
- [剑指offer]正则表达式匹配
- 剑指offer-正则表达式匹配
- 剑指offer|正则表达式匹配
- 《剑指offer》-正则表达式匹配
- 剑指offer-正则表达式匹配
- 【剑指offer】正则表达式匹配
- 【剑指offer】正则表达式匹配
- 剑指offer-正则表达式匹配
- 剑指offer:正则表达式匹配
- 剑指Offer---正则表达式匹配
- 剑指offer 53-正则表达式匹配
- 剑指offer—正则表达式匹配
- 正则表达式匹配(递归+剑指offer)
- 《剑指offer》——正则表达式匹配
- 正则表达式匹配-剑指offer(JAVA版)
- 性能测试必学知识点
- Android 静默安装和智能安装的实现方法
- 【NOIP2013模拟联考2】摘取作物(pick)
- iOS开发 - 第04篇 - 网络 - 02 - JSON解析 & 请求 & 黑酷例子 & HTTP通信
- Apache ActiveMQ 持久化到MySQL数据库
- 剑指offer-正则表达式匹配
- 提问的智慧
- 斯特林大数公式求阶乘位数
- 获取 sdcard 和 内部存储的空间大小
- TextUtils方法介绍
- 关于ES、PES、PS以及TS码流
- 《leetCode》: Implement Trie (Prefix Tree)
- sync命令的作用
- echarts3 柱状图实现