【题目描述】

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的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  3. [LeetCode][Python]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  4. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  6. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  7. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  10. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. WeX5基础

    最近在研究微信app开发,使用的是WeX5,在这里把一些基础知识点记录下来,忘记了可以翻阅查看. 一:开发后端服务 1.建立数据源:窗口--首选项--studio配置--数据源--增加--数据源类型选 ...

  2. spring cron表达式及解析过程

    1.cron表达式 cron表达式是用来配置spring定时任务执行时间的字符串,由5个空格分隔成的6个域构成,格式如下: {秒}  {分}  {时}  {日}  {月}  {周} 每一个域的含义解释 ...

  3. swift学习笔记 - Range、ClosedRange、CountableClosedRange与CountableRange学习

    移动端访问不佳,请访问我的个人博客 在使用swift的过程中字符串操作的时候会用到Range,刚开始使用起来各种别扭,然后发现居然还有ClosedRange.CountableClosedRange和 ...

  4. java for语句执行顺序

    public class test{ public static void main(String[] args) {          int i=0;          for(printChar ...

  5. Linux 动态链接库包含静态链接库的方法

    今天老司机们在讨论一个编译问题  A是一个静态库  C是一个动态库  B是运行程序,能不能将A打包到C 然后B只需要链接C 就可以了. 这个问题我以前在出来zlib库版本冲突的时候有点印象,所以写了个 ...

  6. replace()函数用法

    replace()函数表示将用一个字符串替换字符串中的所出现的特定内容. 语法为:replace(字段1,字段2,字段3),意思为字段3将会替换字段1里与字段2相同的内容  列如: table1 st ...

  7. w3c标准盒模型与IE传统模型的区别

    一.盒子模型(box model) 在HTML文档中的每个元素被描绘为矩形盒子.确定其大小,属性——比如颜色.背景.边框,及其位置是渲染引擎的目标. CSS下这些矩形盒子由标准盒模型描述.这个模型描述 ...

  8. unity调用Android百度地图

    由于个人是Android小白,在这个配置上面被折磨了很久,因此写下这篇文章 工具:eclipse + unity5.6.1 首先去百度地图开发者平台下载你需要的资源,我只需要显示地图和定位,这个时候你 ...

  9. Apache 配置SSL网站

    1. 申请证书    现在可以在阿里云或七牛上申请免费的证书,这里以阿里云为例        进入阿里云证书申请界面    https://www.aliyun.com/product/cas     ...

  10. ubuntu 16.04 配置远程连接

    1.XDMCP远程连接 vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 添加 greeter-show-manual-login=true [X ...