Wildcard Matching
2015-07-18 01:56
309 查看
Implement wildcard pattern matching with support for
Solution:
'?'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
Solution:
class Solution { public: bool isMatch(string s, string p) { if(p.length() == 0) return s.length() == 0; int lenS = s.length(); int lenP = p.length(); int posP = 0, posS = 0, recordP = -1, recordS = -1; while(posS < lenS) { if(posP < lenP && (p[posP] == s[posS]) || p[posP] == '?') { posP++; posS++; } else if(posP < lenP && p[posP] == '*') { recordP = posP; recordS = posS; posP++; continue; } else if(recordP != -1) { posP = recordP + 1; posS = recordS; recordS++; } else return false; } while(posP < lenP && p[posP] == '*') posP++; return posP == lenP && posS == lenS; } };
相关文章推荐
- atitit.html5动画特效----打水漂 ducks_and_drakes
- atitit.html5动画特效----打水漂 ducks_and_drakes
- Linux主机在LNMP环境中同时运行多个PHP版本
- 实现Tab功能的几种方式
- Javascript异步编程的4种方法
- KMP算法的一个C++实现
- atitit.信息系统方案规划 p71.doc
- atitit.信息系统方案规划 p71.doc
- atitit.ajax上传文件的实现原理 与设计
- atitit.ajax上传文件的实现原理 与设计
- jQuery设计思想
- TabHost 和 FragmentTabHost
- jQuery最佳实践
- cocos 关于文件名的各种坑 各种斜杠坑
- C++ const 与 volatile
- 说说面试的那点事
- nyoj255 C小加 之 随机数
- “优衣库视频门”背后之:互联网思维十大解读
- EularProject 40: 计算正整数连接构成无理数的确定位
- Gradle tip #3-Task顺序