題目地址: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));
}
}
};
小數據的測試數據如下: