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());
}
};
												

最新文章

  1. asp.net中web.config配置节点大全详解
  2. NEC学习 ---- 模块 -水平文字链接列表
  3. myeclipse自动import
  4. Testing Multi-Tenancy on a Local Machine
  5. 使用宏定义来减少JNI的繁琐
  6. 什么是Socket,为什么要用Socket
  7. 【QT相关】类头文件解读、QT编辑模式、读取text文本
  8. 判断一个数是否为2的n次幂
  9. 对比jquery获取属性的方法props、attr、data
  10. ios开发-指纹识别
  11. Java-IO之FileDescriptor
  12. Android下VideoView的研究
  13. MySQL使用细节
  14. egret 添加帧动画
  15. linux下无法执行PHP命令,错误 php: command not found
  16. linux下的文件解压命令
  17. kali-rolling安装nessus 7并创建扫描任务教程
  18. ovs源码阅读--元组空间搜索算法
  19. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS
  20. java.lang.IllegalArgumentException: Invalid character found in method name

热门文章

  1. BZOJ-2326 数学作业 矩阵乘法快速幂+快速乘
  2. [IOS UIScrollView+PageControl]信息展示横幅
  3. hihocoder #1058 Combination Lock
  4. 循序渐进Linux 3:Linux下软件安装与管理
  5. IOS基础之 (二) 面向对象思想
  6. poj1787Charlie&#39;s Change(多重背包+记录路径+好题)
  7. Python序列的切片操作与技巧
  8. 常用webshell提权方法总结
  9. Shell脚本获得变量值作为新变量一部分的值
  10. 修改linux文件权限命令:chmod