vector<vector<int>> v; void f(int i, int tar, vector<int>& nums, vector<int>& can) { if ( tar == 0) { v.push_back(nums); return; } if (i == can.size() || tar < 0) return; nums.push_back(can[i]); f(i, tar - can[i], nums, can); nums.pop_back(); f(i + 1, tar, nums, can); } vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<int> ds; f(0, target, ds, candidates); return v; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter