题解 | #牛群的标签和#
牛群的标签和
https://www.nowcoder.com/practice/42ae88bedeb74da99813f6150769d07e
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型vector<vector<>> */ vector<vector<int>> result; vector<int> path; void backTracking(vector<int>& nums,int target,int startIndex,int sumNum) { if (path.size() == 4 && sumNum == target) { vector<int> temp; temp.assign(path.begin(), path.end()); sort(temp.begin(),temp.end()); result.push_back(temp); return; } for (int i = startIndex;i < nums.size();++i) { path.push_back(nums[i]); sumNum += nums[i]; backTracking(nums, target, i + 1, sumNum); sumNum -= nums[i]; path.pop_back(); } } vector<vector<int>> fourSum(vector<int>& nums, int target) { // write code here set<vector<int>> ans; backTracking(nums, target, 0, 0); for (auto it : result) { ans.insert(it); } result.clear(); for (auto it : ans) { result.push_back(it); } return result; } };
可是我只会回溯啊