码迷,mamicode.com
首页 > 其他好文 > 详细

Regular Expression Matching [leetcode]

时间:2015-05-20 23:54:28      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
	bool isMatchCore(string s, string p,int indexS,int indexP){
		if (s[indexS] == ‘\0‘ && p[indexP] == ‘\0‘)
			return true;
		if (s[indexS] != ‘\0‘ && p[indexP] == ‘\0‘)
			return false;
		if (p[indexP + 1] == ‘*‘)
		{
			if (s[indexS] == p[indexP] || (p[indexP] == ‘.‘&&s[indexS] != ‘\0‘))
				return isMatchCore(s, p, indexS, indexP + 2) || isMatchCore(s, p, indexS + 1, indexP + 2)
				|| isMatchCore(s, p, indexS + 1, indexP);
			else
			{
				isMatchCore(s, p, indexS, indexP + 2);
			}
		}
		if (p[indexP] == ‘.‘&&s[indexS] != ‘\0‘)
			return isMatchCore(s, p, indexS + 1, indexP + 1);
		if (p[indexP]==s[indexS])
			return isMatchCore(s, p, indexS + 1, indexP + 1);

		return false;
	}
	bool isMatch(string s, string p) {
		if (s == "" && p == "")
			return false;
		int indexS = 0,indexP = 0;
		return isMatchCore(s, p, indexS, indexP);
	}
};

  显示超时了,暂时还没想到解决办法。

Regular Expression Matching [leetcode]

标签:

原文地址:http://www.cnblogs.com/chengxuyuanxiaowang/p/4518452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!