Leetcode -- Wildcard Matching
2015-10-29 16:23
363 查看
Implement wildcard pattern matching with support for
分析:
递归算法会超时,因此采用了while循环的写法:
整体思路还是在出现\(\star\)的时候对s字符串往后的每个位置进行匹配。
'?'and
'*'.
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 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", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab", "c*a*b") → false
分析:
递归算法会超时,因此采用了while循环的写法:
class Solution { public: bool isMatch(string s, string p) { size_t i=0,j=0,star=-1,beg; if(p.empty()) return s.empty(); while(i<s.size()) { if(s[i]==p[j]||p[j]=='?') { i++;j++; continue; } if(p[j]=='*') { star=j++; beg=i; continue; } if(star>=0) { j=star+1; i=++beg; continue; } return 0; } while(p[j]=='*') j++; return j==p.size(); } };
整体思路还是在出现\(\star\)的时候对s字符串往后的每个位置进行匹配。
相关文章推荐
- 极光推送CTO黄鑫:技术人员要建立自己的知识图谱
- 图文详细解说DevExpress 2015新版亮点【附文档下载】
- table-layout为fixed时,表格长度平均分配的现象解决
- 上google搜索
- 打印Java main方法执行的命令参数代码
- github笔记2
- 个人笔记 js 12 js返回上一页
- C# 给PDF文件添加水印
- 不要手动StopWatch了,让BenchmarkDotNet帮你
- span设置宽度有效无效问题
- 个人笔记 js 10 计算当前访客数
- hdu 1166 敌兵布阵(线段树单点更新(a位置的值+/-b)+区间求和)
- Android启动外部Activity时必须添加的Flag
- AfxGetApp()的使用
- 深入剖析Go语言编程中switch语句的使用
- Android高效加载大图、多图解决方案(LruCache)
- Emoji表情符号兼容方案
- 基于Disruptor游戏服务器消息总线的设计
- 欢迎使用CSDN-markdown编辑器
- jQuery.i18n.properties实现前端国际化