Handle KSum yet still accepted with best performance 16ms in C++
class Solution {\n    private:\n        const int K = 4;\n        int size = 0;\n        void search(vector<int>& nums, int pos, int k, int target, vector<int>&
    class Solution {\n    private:\n        const int K = 4;\n        int size = 0;\n        void search(vector<int>& nums, int pos, int k, int target, vector<int>& v, vector<vector<int>>& vv)\n        {\n            if(k == 2)\n            {\n                int l = pos, r = size-1;\n                while(l < r)\n                {\n                    int t = nums[l]+nums[r];\n                    if(t > target) r--;\n                    else if(t < target) l++;\n                    else \n                    {\n                        v[K-2] = nums[l++];\n                        v[K-1] = nums[r--];\n                        vv.push_back(v);\n                        while(l<r && nums[l]==nums[l-1]) l++; //avoid duplicates;\n                        while(l<r && nums[r]==nums[r+1]) r--; //avoid duplicates;\n                    }\n                }\n            }\n            else\n            {\n                for(int top = size-k; pos <= top; ++pos)\n                {\n                    int sum = 0;\n                    for(int i = 0; i < k; i++) sum += nums[pos+i]; \n                    if(sum > target) break; //avoid futile searching;\n                    sum = nums[pos];\n                    for(int i = 0; i < k-1; ++i) sum += nums[size-1-i];\n                    if(sum < target) continue; //avoid futile searching;\n                    v[K-k] = nums[pos];\n                    search(nums, pos+1, k-1, target-nums[pos], v, vv);\n                    while(pos<=top && nums[pos+1]==nums[pos]) pos++; //avoid duplicates;\n                }\n            }\n        }\n    public:\n        //AC - 16ms - handle kSum;\n        vector<vector<int>> fourSum(vector<int>& nums, int target) \n        {\n            sort(nums.begin(), nums.end());\n            size = nums.size();\n            vector<vector<int>> vv;\n            vector<int> v(K, 0);\n            search(nums, 0, K, target, v, vv);\n            return vv;\n        }\n    };
['Depth-First Search', 'C++']