LeetCode第44题之 Wildcard Matching
来源:互联网 发布:韩国社交软件sns 编辑:程序博客网 时间:2024/06/05 15:58
思想主要参考这里,但那里是用char *保存的字符串,该字符串最后以’\0’字符结尾,故上述代码中p字符串不会越界。而现在字符串是用的string保存,需要讨论p字符串的下标是否越界。
C++代码:
#include <iostream>#include <string>#include <vector>using namespace std;class Solution {public: bool isMatch(string s, string p) { //ps指向s当前待比较的字符 int ps = 0; //pp指向p当前待比较的字符 int pp = 0; //ss如果非负,则如果当前某个字符不匹配,则ps应回到++ss的位置继续比较 int ss = -1; //p_start如果非负,则如果当前某个字符不匹配,则pp应回到p_start+1的位置继续比较 int p_start = -1; while (ps < s.size()) { if ( pp < p.size()) { if (s[ps] == p[pp] || '?' == p[pp]) { ++ps; ++pp; } else if ('*' == p[pp]) { p_start = pp++; ss = ps; } else { //如果前面出现了'*'号 if (p_start >= 0) { ps = ++ss; pp = p_start+1; } else { return false; } } } //如果p字符串便历到末尾,而s没有到末尾 else if (pp == p.size() && ps < s.size()) { //当匹配不下去的时候如果前面有'*'(即p_start >= 0),让‘*’再多匹配一个字符,更新pp和ps再开始匹配 if (p_start >= 0) { ps = ++ss; pp = p_start+1; } else { return false; } } //p和s同时到末尾 else { return true; } }//while //如果s到末尾了而p没有到末尾 for (;pp < p.size();++pp) { if ('*' != p[pp]) { return false; } } return true; }};int main(){ Solution s; cout<<s.isMatch("abbb", "a*b")<<endl; return 0;}
0 0
- LeetCode第44题之 Wildcard Matching
- 【leetcode】第44题 Wildcard Matching 题目+解析+代码
- LeetCode之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
- Window和WindowManager思维导图
- BZOJ2795: [Poi2012]A Horrible Poem
- 并查集
- js 和 native 如何交换数据
- redis初步接触
- LeetCode第44题之 Wildcard Matching
- AES解密
- Floodlight源码阅读之内存数据库
- mysql ERROR 1045 错误的解决办法,很有用啊
- ios流媒体直播整个框架介绍(HLS、RTSP)
- Python GUI开发(一) 环境搭建
- Android 做项目总结
- think in java笔记:How a garbage collector works
- 使用Spring框架实现RESTful