Regular Expression Matching [leetcode]

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);
	}
};

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

posted @ 2015-05-20 22:46  程序员小王  阅读(117)  评论(0编辑  收藏  举报