LeetCode(Oct 19 '12):Distinct Subsequences

題目地址:http://leetcode.com/onlinejudge#question_115

題目要求如下:

先用了遞歸,小數據過了,大數據時間超限,一會再想DP的辦法。

遞歸代碼如下(可以把尾遞歸去掉):

class Solution {
public:
    int count;
    int numDistinct(string S, string T)
 	{
		count=0;
		recurD(S,T);
		return count;
    }

    void recurD(string S,string T)
	{
		if(T.length()==0)
		{
			count++;
			return;
		}

		if(S.length()==0||S.length()<T.length()) return;

		while(S.find(T[0])!=string::npos)
		{
			S=S.substr(S.find(T[0])+1);
			recurD(S,T.substr(1));
		}
	}
};


小數據的測試數據如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章