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-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)

    潇爷昨天刚刚讲完...感觉得还可以...对着模板打了个模板...还是不喜欢用指针.... 1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Lim ...

  2. MVC传值汇总

     方法一: Url传参是通过Get的方式,一般我们都是通过一定规则的Url来传参.比如下面的URL. http://localhost/contorller/action/?Params1=a& ...

  3. kindeditor粘贴word文档内容时去除格式的方法?如何设置为默认无文本格式呢?

    打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可. 设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴.

  4. easyloader.js源代码分析

    http://www.cnblogs.com/jasonoiu/p/easyloader_source_code_analysis.html Jquery easyui是一个javascript UI ...

  5. 浏览器查看cookie

    今天总结下,教你怎样查看一些浏览器的Cookie,比如IE.Firefox.Chrome的Cookies等.下面分块介绍,以后会关注一些没有讲到的浏览器获取Cookie的方法. 1.Firefox浏览 ...

  6. DOM 元素 属性和方法

    console.dir() namespaceURI: "http://www.w3.org/1999/xhtml" nextElementSibling: null nextSi ...

  7. apache AddDefaultCharset

    AddDefaultCharset 指令 说明 当应答内容是text/plain或text/html时,在HTTP应答头中加入的默认字符集 语法 AddDefaultCharset On|Off|ch ...

  8. 调用{dede:likewords}为dedecms添加相关搜索词

    经常看到一些大型的网站会设置相关搜索,即使访客搜索的内容在本站暂时没有,它们也会展示一些其他搜索关键词,引导用户去点击查看,增加pv,提高用户体验:如果没有这些相关搜索,游客没有找到自己想要的内容就直 ...

  9. Linux守护进程的启动方法

    导读 “守护进程”(daemon)就是一直在后台运行的进程(daemon),通常在系统启动时一同把守护进程启动起来,本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的由来 Web应用写好后 ...

  10. 不同版本的name可以重复

    - validates :name, presence: true, uniqueness: { conditions: -> { where(:state.ne => 2) } }, l ...