LeetCode 44. Wildcard Matching
来源:互联网 发布:黄金家族软件 编辑:程序博客网 时间:2024/04/27 18:02
同样是一道贪心的题目。但是我首次写的代码是用的回溯,结果超时了。问题可能就出在这个回溯上。所以就写了一个数组表示的。AC。
class Solution {public: bool isMatch(string s, string p) { int presp,press; bool flag = false; int i,j; i=j=0; while(i<s.size()) { if(p[j]=='?'){i++;j++;} else if(p[j]=='*'){ presp = ++j; press = i; flag = true; }else{ if(s[i]==p[j]){i++;j++;} else if(flag){ j = presp; i = (++press); }else return false; } } while(p[j]=='*')j++; return j==p.size(); }};
我再把超时的贴上:
class Solution {public: bool isMatch(string s, string p) { if(s.empty()) { if(p[0]=='*'&&p.find_first_not_of('*',1)==string::npos)return true; return p.empty(); } if(p[0]=='*') { while(!s.empty()) { if(isMatch(s.substr(0,s.size()),p.substr(1,p.size()-1)))return true; s=s.substr(1,s.size()-1); } return isMatch(s,p.substr(1,p.size()-1)); } else if((!s.empty()&&p[0]=='?')||s[0]==p[0]) { return isMatch(s.substr(1,s.size()-1),p.substr(1,p.size()-1)); } return false; }};
0 0
- LeetCode --- 44. Wildcard Matching
- [Leetcode] 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- Leetcode:44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode.44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- Leetcode-44. Wildcard Matching
- [LeetCode] 44. Wildcard Matching
- [leetcode] 44. Wildcard Matching
- [LeetCode]--44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- 44. Wildcard Matching -- LeetCode
- LeetCode 44. Wildcard Matching
- HDU 5826(水题,物理公式推导,积分公式推导直接出结果)
- 动态规划 - 九度OJ 1480
- css 利用阴影给字体加粗
- DataGuard主备归档存在gap的处理办法
- HDU 1232 畅通工程 (并查集)
- LeetCode 44. Wildcard Matching
- 关于springMVC中使用session的理解
- Spark与Pandas中DataFrame对比(详细)
- C#学习笔记(三)
- 派生类的对象访问基类中被派生类覆盖或隐藏了的函数或变量
- 快捷键
- 单链表 双链表 环链表的简单操作
- Codeforces Round #367 (Div. 2) [C] Hard Problem
- POJ1584 几何 凸包判断 点和多边形的位置关系