Re: [闲聊] 每日leetcode

楼主: DJYOMIYAHINA (通通打死)   2024-08-13 23:27:52
直接set<vector<int>>会TLE
backtracking的剪枝还是没很熟ㄚ
这类的没啥印象
class Solution {
public:
void helper(vector<int>& candidates, vector<vector<int>>& ans,
vector<int>& cur, int idx, int target) {
if(target == 0) {
ans.push_back(cur);
return;
}
if(idx >= candidates.size() || target<0) {
return;
}
int pre = -1;
for(int i=idx; idx<candidates.size(); idx++) {
if(candidates[idx] == pre) {
continue;
}
cur.push_back(candidates[idx]);
helper(candidates,ans,cur,idx+1,target-candidates[idx]);
cur.pop_back();
pre = candidates[idx];
}
return;
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> cur;
helper(candidates,ans,cur,0,target);
return ans;
}
};
楼主: DJYOMIYAHINA (通通打死)   2024-08-13 23:30:00
如果要用unordered_set<vector<int>> 要自己写hash
作者: CCapocalypse (CCinfinity)   2024-08-13 23:42:00
写得跟我几乎一样耶 不过交出去感觉速度排名偏慢

Links booklink

Contact Us: admin [ a t ] ucptt.com