(剑指Offer)面试题53:正则表达式匹配
题目:
请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
思路:
假设字符串为str,模式串为pattern,考虑以下情况:
A. 模式串下一个字符为*,即*(pattern+1)=='*':
如果当前字符匹配,即*str=*pattern或者*str='.' && *pattern!='\0',三种可能:
1、模式串当前字符出现0次,即*表示当前字符出现0次,则str=str,pattern=pattern+2;
2、模式串当前字符出现1次,即*表示当前字符出现1次,则str=str+1,pattern=pattern+2;
3、模式串当前字符出现2次或2次以上,即*表示当前字符出现2次或以上,则str=str+1,pattern=pattern;
如果当前字符不匹配,则只能让*表示当前字符出现0次,则str=str,pattern=pattern+2;
B. 模式串下一个字符不为*
如果当前字符匹配,即*str=*pattern或者*str='.' && *pattern!='\0',则str=str+1,pattern=pattern+1.
代码:
#include <iostream>
using namespace std;
bool RegMatchCore(const char* str, const char* pattern){
if(*str=='\0' && *pattern=='\0')
return true;
if(*str!='\0' && *pattern=='\0')
return false;
if(*(pattern+1)=='*'){
if(*str==*pattern || (*str!='\0' && *pattern=='.'))
return RegMatchCore(str,pattern+2) || RegMatchCore(str+1,pattern+2) || RegMatchCore(str+1,pattern);
else
// ignore *
return RegMatchCore(str,pattern+2);
}
if(*str==*pattern || (*str!='\0' && *pattern=='.'))
return RegMatchCore(str+1,pattern+1);
return false;
}
bool RegMatch(const char* str, const char* pattern){
if(str==NULL || pattern==NULL)
return false;
return RegMatchCore(str,pattern);
}
int main()
{
char str[] = "aaa";
char pattern1[]="ab*ac*a";
char pattern2[]="ab*a";
cout << RegMatch(str,pattern1) << endl;
cout << RegMatch(str,pattern2) << endl;
return 0;
}
在线测试OJ:
http://www.nowcoder.com/books/coding-interviews/45327ae22b7b413ea21df13ee7d6429c?rp=3
AC代码:
class Solution {
public:
bool match(char* str, char* pattern)
{
if(str==NULL || pattern==NULL)
return false;
return matchCore(str,pattern);
}
bool matchCore(const char* str,const char* pattern){
if(*str=='\0' && *pattern=='\0')
return true;
if(*str!='\0' && *pattern=='\0')
return false;
if(*(pattern+1)=='*'){
if(*str==*pattern || (*str!='\0' && *pattern=='.'))
return matchCore(str,pattern+2)||matchCore(str+1,pattern+2)||matchCore(str+1,pattern);
else
return matchCore(str,pattern+2);
}
if(*str==*pattern || (*str!='\0' && *pattern=='.'))
return matchCore(str+1,pattern+1);
return false;
}
};
(剑指Offer)面试题53:正则表达式匹配的更多相关文章
- 【剑指Offer】52、正则表达式匹配
题目描述: 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹 ...
- 剑指Offer:面试题15——链表中倒数第k个结点(java实现)
问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- 剑指Offer——笔试题+知识点总结
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
随机推荐
- 在SpringMVC Controller中注入Request成员域
主题 在工作中遇到1个问题....我们定义了一个Controller基类,所有Springmvc自定义的controller都继承它....在它内部定义一个@Autowired HttpServlet ...
- Am335x u-boot 启动过程中的系统频率配置
Am335x的时钟结构分为:ADPLLS和ADPLLLJ 1.ADPLLS用来配置Core_CLK,Dispaly_clk,ARM系统CLK(mpu_clk),DDR PLLs_clk 2.ADPLL ...
- MAC OS 10.10.5虚拟机免费下载(可安装Xcode7)
MAC OS 10.10.5虚拟机免费下载(可安装Xcode7) MAC OS 10.10.5虚拟机免费(可安装Xcode7)下载地址:链接: http://pan.baidu.com/s/1dD ...
- elasticsearch中ik词库配置远程热加载
1. 修改 IKAnalyzer.cfg.xml 配置文件中的<entry key="remote_ext_dict">http://127.0.0.1/xxx.txt ...
- jQuery before 和 after
A.after(B) ==== B.insertAfter(A) B 放在 A 的后面A.before(B) ==== B.insertBefore(A) B 放在 A 的前面 A.append(B) ...
- PHP 笔记——会话控制
1. Session的操作 1.1 启动 Session session_start(void):bool 1.2 注册 Session 会话变量启动后,全部被保存在全局数组$_SESSION[]中. ...
- 「NOI2017」游戏
「NOI2017」游戏 题目描述 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 \(A\).\(B\).\ ...
- HTML && xml 的区别
HTML && xml 的区别 HTML 超文本标记语言 xml 可扩展标记语言 jsp 表面是一个HTML页面,本质是一个servlet HTML 超文本标记语言 HTML 是一 ...
- 内功心法 -- java.util.LinkedList<E> (5)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
- Bipolar transistor boosts switcher's current by 12 times
The circuit in Figure 1 uses a minimal number of external parts to raise the maximum output current ...