[leetcode] Regular Expression Matching
2015-08-20 15:14
411 查看
原题如下:
'.' 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
本题主要处理*号,先把代码贴上,C++,运行时间4ms
class Solution { public: bool isMatch(string s, string p) { int sSize = s.size(),pSize = p.size(); int storeSize = (1+sSize)*(1+pSize); bool *flag = new bool[storeSize]; flag[0]=true; for(int i=1;i<storeSize;i++) flag[i]=false; for(int i=0;i<pSize;i++) { if('*'==p[i]) { for(int j=0;j<=sSize;j++) { flag[(i+1)*(sSize+1)+j] = flag[(i-1)*(sSize+1)+j] || flag[(i)*(sSize+1)+j] || (j-1>=0 && flag[(i+1)*(sSize+1)+j-1]&&(p[i-1]==s[j-1]|| p[i-1]=='.')); } } else { for(int j=1;j<=sSize;j++) { flag[(i+1)*(sSize+1)+j] = flag[i*(sSize+1)+j-1] && (p[i]==s[j-1] || p[i]=='.'); } } } bool resutl = flag[storeSize-1]; delete []flag; return resutl; } };
使用动态规划算法,有空再写写思路
相关文章推荐
- Python 使用正则表达式 - 2
- 获取ios系统的进程列表(pid, names, paid, status)
- iOS开发——多线程篇——NSOperation(基于GCD多线程编程),下载图片并合成新图片
- 编写高质量代码改善C#程序的157个建议——建议95:避免在构造方法中调用虚成员
- Gamebryo 游戏引擎分析(一)整体设计
- haproxy
- Python3字符串学习教程
- 动态数组封装实现向量类
- TCP与UDP的区别
- object-c语言的nonatomic,assign,copy,retain的区别
- jQuery的deferred对象详解
- CodeForces-447C DZY Loves Sequences
- Pixhawk---烧写FMU/IO bootloader
- 修改 SVN 账户密码的方法
- Tomcat配置JNDI数据源
- clipsTobounds属性的作用
- DOM事件简介
- js实现按钮颜色渐变动画效果
- 随笔(2015-18-19)
- 使用eclipse开发web项目中常见的中文乱码问题总结