LeetCode之Regular Expression Matching
【题目描述】
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
【解决思路】
大概可以运用动态规划的思想,将其分为几种情况进行迭代即可解决:
1、当正则表达式为空时,只需要判断原字符是否为空即可。
2、当都不为空时,需要逐字符判断即可,首先判断第一个字符是否相等,其次需要根据第二个字符是否为*分为两种情况:
1)第二个字符为*,则需要判断判断*表示第一个字符出现0次或者1次的情况。
2)第二个字符不为*,则判断直接判断第二个结果。
【代码】
public class Solution
{
/**
* @param s 原始数据
* @param p 正则表达式,仅支持*、.
* @return
*/
public boolean isMatch(String s, String p) {
if(p.isEmpty()) return s.isEmpty(); boolean first_match = (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')); if(p.length() >= 2 && p.charAt(1) == '*')
{
return isMatch(s, p.substring(2)) || (first_match && isMatch(s.substring(1), p));
}
else
{
return first_match && isMatch(s.substring(1), p.substring(1));
}
} public static void main(String[] args)
{
System.out.println(new Solution().isMatch("abcccc", "c*abc*d"));
}
}
LeetCode之Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- [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
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- SDUT oj 2610
/*题目大意:输入一序列n个数字,然后输入m个询问,每个询问包含左边区间和右边区间,还有a和b,问你这个区间内有几个数大于等于a且小于等于b 做法:树状数组,先求出这个区间内有几个数小于a,然后求这个 ...
- xstream中几个注解的含义和用法(转)
XStream是个很强大的工具,能将Java对象和xml之间相互转化.xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数.它调用方式也非常简单:从xml对象转化为jav ...
- Commons Configuration之三Properties文件
转载自(https://my.oschina.net/u/2000201/blog/486653) Properties文件是流行的应用程序配置文件.当然,Commons Configuration支 ...
- Linux 下部署Django项目
Linux 下部署Django项目 说明:本文所使用的环境为CentOS 6+Python2.7+Django1.11 安装Django.Nginx和uWSGI 1.确定已经安装了2.7版本的Py ...
- springboot2.1.3集成webservice及错误No operation was found with the name {...}解决办法
1.项目使用springboot 2.1.3版本,集成webservice使用的依赖如下 <parent> <groupId>org.springframework.boot& ...
- vs asp.net 给所有邮箱发邮件,案例,源代码,c#
//发送邮箱 MailMessage mailObj = new MailMessage(); mailObj.From = new MailAd ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- [小问题笔记(三)] SVN树冲突(Tree Conflict),文件不能提交的解决办法
传说中SVN的树冲突是由不同开发者删除文件,移动文件神马的造成的. 我们遇到的情况是: 开发人员小B移动了项目中几个文件然后提交.开发人员小L更新项目至最新版本. 获取到移动后的文件则显示文件已被修改 ...
- Math.Round 四舍五入问题 解惑 !
前言: 一定要说 坑爹的微软! 坑爹的微软! 坑爹的微软! 重要的事情说 三遍 ! 摘录 SMDN 示例 下面的示例演示就近舍入. C# C++ VB Math.Round(3.44, 1); ...
- 编译binutil包报错cc: error trying to exec 'cc1obj': execvp: No such file or directory
在http://forums.fedoraforum.org/showthread.php?t=267449中找到的解决方法 $LFS/sources/binutils-2.15.91.0.2/gpr ...