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 ...
随机推荐
- MySQL主从不一致修复
场景: 线上正在服务的库由于紧急主从切换导致主从不一致,报错信息如下: Last_Error: Coordinator stopped because there were error(s) in t ...
- Python2 显示 unicode
用户想要看的是 u'中文' 而不是 u'\u4e2d\u6587',但是在 Python2 中有时并不能实现. 转译 转义字符是这样一个字符,标志着在一个字符序列中出现在它之后的后续几个字符采取一种替 ...
- mysql ErrorNo:1449
ErrorMsg:The user specified as a definer ('root'@'%') does not exist解决方法:权限问题,授权 给 root 所有sql 权限 mys ...
- 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作
/** * 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作 */ public class ThreadTest { private int j; public static vo ...
- Java HashMap工作原理及实现(转载)
https://yikun.github.io/2015/04/01/Java-HashMap工作原理及实现/
- Pandas 通过追加方式合并多个csv
常用合并 通常用pandas进行数据拼接.合并的方法有: pandas.merge() pandas.concat() pandas.append() 还有一种方式就是通过 pd.to_csv() 中 ...
- django一些配置与ORM
- mysql以下c连接mysql数据库
1.安装sudo yum install mysql-devel 安装组件和库 2. #include <stdio.h> #include <stdlib.h> #incl ...
- mysql 内置功能 函数 date_format函数
创建数据库db12 create database db12 charset=utf8; use db12; 准备表和记录 CREATE TABLE blog ( id INT PRIMARY KEY ...
- mysql-5.6.22的安装步骤
一.环境与下载地址: 1.系统下载地址: http://mirrors.sohu.com/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso 2 ...