18. 四数之和
class Solution {
public:
vector
<vector
<int>> fourSum(vector
<int>& nums
, int target
) {
vector
<vector
<int>> result
;
vector
<int> tempresult
;
sort(nums
.begin(),nums
.end());
Search(nums
,target
,0,4,tempresult
,result
);
return result
;
}
void Search(vector
<int>& nums
,int target
,int start
,int count
,vector
<int> &tempresult
,vector
<vector
<int>> &result
){
if(target
==0&&count
==0){
result
.push_back(tempresult
);
return;
}
if(count
==0) return;
for(int i
=start
;i
<nums
.size();i
++){
if((nums
[i
]>target
&&nums
[i
]>0)) return;
if(i
==start
||nums
[i
]!=nums
[i
-1]){
tempresult
.push_back(nums
[i
]);
Search(nums
,target
-nums
[i
],i
+1,count
-1,tempresult
,result
);
tempresult
.pop_back();
}
}
}
};
转载请注明原文地址:https://blackberry.8miu.com/read-34699.html