关于递归回溯法的时间复杂度的计算
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ret;
vector<int> tmp;
help(ret,tmp,candidates,target,0);
return ret;
}
void help(vector<vector<int>> &ret,vector<int> &tmp,vector<int> &candidates,int target,int index){
if(target==0){
ret.push_back(tmp);
return;
}
for(int i=index;i<candidates.size()&&target>=0;i++){
tmp.push_back(candidates[i]);
help(ret,tmp,candidates,target-candidates[i],i);
tmp.pop_back();
}
}
};
这是leetcode上的39. Combination Sum
想请问这种递归算法时间复杂度是多少?如何计算