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 ...
随机推荐
- 巨蟒python全栈开发数据库前端7:jQuery框架
每个人的标准不同,看法等等,认识,价值观有所不同,促成了这些矛盾. 1.select例子 <!DOCTYPE html> <html lang="en"> ...
- Linux基础命令(三)
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group >/1.txt 2) 将用户信息数据库文件和用户 ...
- weal woe
He is worth no weal that can bide no woe. 禁不起吃苦的人不配得到幸福 有句谚语叫No weal without woe 福兮祸所伏 ; 祸兮福所倚 weal和 ...
- J2EE之JPA
POJO(plain old java object普通java类):具有setter/getter方法的Java类就称为POJO POJO转化为实体,手工使用标记, @Entity public c ...
- 01 javaSe 01 抽象类和接口
抽象类 接口 目录(?)[-] 1 抽象类与接口是面向对象思想层面概念不是程序设计语言层面概念 2 抽象类是本体的抽象接口是行为的抽象 3 C中抽象类与接口的探讨 目录(?)[+] ...
- 采集baidu搜索信息的java源代码实现(大部分转发,少量自己修改)(使用了htmlunit和Jsoup)(转发:https://blog.csdn.net/zhaohang_1/article/details/44731039)
1.maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- C语言自带快速排序对比插入排序
#include <stdio.h> #include <stdlib.h> #include <time.h> void getRandomArr (int ar ...
- 关于shared pool的深入探讨(三)
基本命令: ALTER SESSION SET EVENTS 'immediate trace name LIBRARY_CACHE level LL'; 其中LL代表Level级别,对于9.2.0及 ...
- 『NiFi 学习之路』资源 —— 资料汇总
一.概述 由于 NiFi 是一个比较新的开源项目,国内的相关资料少之又少. 加之,大家都知道,国内的那么些个教程,原创都只是停留在初级使用阶段,没有更多深入的介绍. 再者,其余的文章不是东抄抄就是西抄 ...
- Educational Codeforces Round 11B. Seating On Bus 模拟
地址:http://codeforces.com/contest/660/problem/B 题目: B. Seating On Bus time limit per test 1 second me ...