【题目描述】

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. JS正则表达式从入门到入土(4)—— 预定义类与边界

    预定义类 正则表达式提供预定义类来匹配常见的字符类 字符 等价类 含义 . [^\r\n] 除了回车符和换行符以外的所有字符 \d [0-9] 数字字符 \D [^0-9] 非数字字符 \s [\t\ ...

  2. Eclipse配置多个jdk

    Eclipse配置多个jdk 步骤: 1,打开windows > Preferences: 2,点击“Add”,新增jdk,选择“Standard VM”: 3,下一步,选择对应版本的jer: ...

  3. 20145321 《Java程序设计》第一周学习总结

    20145321 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 1.三大平台:Java SE.Java EE .Java ME 2.Java SE:由JVM.JRE.JDK ...

  4. LeetCode (262):Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  5. Mac Book Air 上用 Vmware Fusion 8 pro 安装 CentOS7

    一. 准备工作: 1. 安装Vmware Fusion (略) 2. 下载CentOS-7-x86_64-Minimal http://isoredirect.centos.org/centos/7/ ...

  6. ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password

      What I don't fully understand is the use of ClientId and Secret vs Username and Password. The code ...

  7. mybatis动态sql中的bind绑定

    知识点:bind在模糊查询中的用法 在我的博客    mybatis中使用mysql的模糊查询字符串拼接(like) 中也涉及到bind的使用 <!-- List<Employee> ...

  8. select * from table_name where 1=1的

    我们先来看看这个语句的结果:select * from table where 1=1,其中where 1=1,由于1=1永远是成立的,返回TRUE,条件为真:所以,这条语句,就相当于select * ...

  9. java开发设计六大基本原则

    1.遵循单一职责原则 定义:不要存在多于一个导致类变更的原因.通俗的说,即一个类只负责一项职责.一个类只专注于做一件事: 高内聚,低耦合: 实例: 普通的手表如果有一个指针坏了,那么手表将不再转动,而 ...

  10. jquery插件之jquery-ui

    1.jQuery UI是在jQuery的基础上,利用jQuery的扩展性,设计的插件.提供了一些常用的界面元素,如:模态框.日期控件等. 2.下载地址:http://jqueryui.com/down ...