C++ Solution, 8ms, beats 97%
Runtime: 8 ms, faster than 97.93% of C++ online submissions for 4Sum.\nMemory Usage: 8.9 MB, less than 100.00% of C++ online submissions for 4Sum.\n\n```\nclass
Runtime: 8 ms, faster than 97.93% of C++ online submissions for 4Sum.\nMemory Usage: 8.9 MB, less than 100.00% of C++ online submissions for 4Sum.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        \n        vector<vector<int>> res;\n        sort(nums.begin(), nums.end());\n        \n        int n = nums.size();\n        for(int i=0; i<n-3; i++)\n        {\n            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3] > target) break;\n            if(nums[i]+nums[n-1]+nums[n-2]+nums[n-3] < target) continue;\n            \n            for(int j=i+1; j<n-2; j++)\n            {\n                if(nums[j]+nums[j+1]+nums[j+2] > target - nums[i]) break;\n                if(nums[j]+nums[n-1]+nums[n-2] < target - nums[i]) continue;\n                int newTarget = target - nums[i] - nums[j];\n                int start = j+1;\n                int end = n-1;\n                \n                while(start < end)\n                {\n                    if(nums[start] + nums[end] < newTarget)\n                        start++;\n                    else if(nums[start] + nums[end] > newTarget)\n                        end--;\n                    else\n                    {\n                        res.push_back(vector<int> {nums[i], nums[j], nums[start], nums[end]});\n                        start++;\n                        end--;\n                        while(start < end && nums[start] == res.back()[2]) start++;\n                        while(start < end && nums[end] == res.back()[3]) end--;\n                    }\n                }\n                \n                while(j+1 < n && nums[j] == nums[j+1]) j++;\n            }\n            \n            while(i+1 < n && nums[i] == nums[i+1]) i++;\n        }\n        \n        return res;\n    }\n};
[]