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

这道题初学者或多或少都参考了网上的答案,主要难点有以下
一、要理解正则表达式中.和*的含义,“.”代表任意字符,但是“a*”代表“”,“a”,“aa”,“aaa”……这一点比较容易让人犯迷糊
二、必须完全比配,即isMatch("aa","aaa") → false
三、第一个参数String s 是不含有“.”和“*”,因此不要把程序复杂化
Java实现如下:
static public boolean isMatch(String s, String p) {
// 如果从s长度入手,s.length() == 0时("","a*")、("","a*b*")都会返回true
if (p.length() == 0)
return s.length() == 0;
else if (p.length() == 1)
return s.length() == 1&& (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0));
if (p.charAt(1) != '*') {
if (s.length() == 0|| (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0)))
return false;
return isMatch(s.substring(1), p.substring(1));
}
else if (isMatch(s, p.substring(2)))
return true;
else {
int i = 0;
while (i < s.length()&& (p.charAt(0) == '.' || p.charAt(0) == s.charAt(i))) {
if (isMatch(s.substring(i + 1), p.substring(2)))
return true;
i++;
}
}
return false;
}
C++面向对象:
 class Solution {
public:
bool isMatch(string s, string p) {
if (p.length() == )
return s.length() == ;
else if (p.length() == )
return s.length() == && (p[] == '.' || s[] == p[]);
if (p[] != '*') {
if (s.length() == || (p[] != '.' && s[] != p[]))
return false;
return isMatch(s.substr(), p.substr());
}
else if (isMatch(s, p.substr()))
return true;
else {
int i = ;
while (i < s.length() && (p[] == '.' || p[] == s[i])) {
if (isMatch(s.substr(i + ), p.substr()))
return true;
i++;
}
}
return false;
}
};

C++ 面向过程(效率高):

 class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (!p[])
return !s[];
if (!p[] || p[] != '*')
return s[] && (p[] == '.' || s[] == p[])&& isMatch(++s, ++p);
while (s[] && (p[] == '.' || s[] == p[]))
if (isMatch(s++, p + ))
return true;
return isMatch(s, p + );
}
bool isMatch(string s, string p) {
return isMatch(s.data(), p.data());
}
};
												

【JAVA、C++】LeetCode 010 Regular Expression Matching的更多相关文章

  1. LeetCode 010 Regular Expression Matching

    题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  7. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  2. DRUPAL-PSA-CORE-2014-005 && CVE-2014-3704 Drupal 7.31 SQL Injection Vulnerability /includes/database/database.inc Analysis

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Use Drupal to build everything from perso ...

  3. 通过HTTP协议实现多线程下载

    1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处     下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...

  4. jQueryEasyUI DateBox的基本使用

    http://www.cnblogs.com/libingql/archive/2011/09/25/2189977.html 1.基本用法 代码: 1 2 3 4 5 6 7 8 9 10 11 1 ...

  5. latin1

    Latin1是ISO-8859-1的别名,有些环境下写作Latin-1.ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII ...

  6. Common Pitfalls In Machine Learning Projects

    Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...

  7. 快速tab应用

    ZCTabNav-master https://github.com/zcsoft/ZCTabNav 层次构架清楚,很适合快速,导入

  8. Visual Studio Online Integrations-Customer support

    原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  9. PHP中array_chunk的用法

    转自:http://cn2.php.net/manual/zh/function.array-chunk.php (PHP 4 >= 4.2.0, PHP 5) array_chunk — 将一 ...

  10. lamp 网站打不开,不显示也不报错,

    原因是该网站的编程员,习惯简写,<? ?>;而服务器版本的php.ini 默认不支持只支持<?php ?>这种格式. 解决方法vim /usr/loacl/php/etc/ph ...