kSum Java Solution, intuitive and clean.
The idea is to decompose the K sum problem into classic 2 sum problem. \nIn Two sum problem, we use head and tail pointer to find the solution.\njava\n    publi
The idea is to decompose the K sum problem into classic 2 sum problem. \nIn Two sum problem, we use head and tail pointer to find the solution.\n```java\n    public List<List<Integer>> fourSum(int[] nums, int target) {\n        List<List<Integer>> res = new LinkedList<>();\n        if(nums == null || nums.length == 0){\n            return res;\n        }\n        Arrays.sort(nums);\n        return kSum(4, nums,0,target);\n    }\n\n    public List<List<Integer>> kSum(int k, int[] nums, int startIndex, int target){\n        List<List<Integer>> res = new LinkedList<>();\n        if(k > nums.length - startIndex){\n            return res;\n        }\n        if(k == 2){\n            int i = startIndex, j = nums.length-1;\n            while(i < j){\n                if(nums[i] + nums[j] == target){\n                    List<Integer> temp = new LinkedList<>();\n                    temp.add(nums[i]);\n                    temp.add(nums[j]);\n                    res.add(temp);\n                    while(i < j && nums[i] == nums[++i]);\n                    while(i < j && nums[j] == nums[--j]);\n                }\n                else if(nums[i] + nums[j] < target){\n                    i++;\n                }\n                else{\n                    j--;\n                }\n            }\n            return res;\n        }\n        else{\n            for(int i = startIndex; i < nums.length; i++){\n                if(i > startIndex && nums[i] == nums[i-1]){\n                    continue;\n                }\n                List<List<Integer>> tempLists = kSum(k - 1, nums, i + 1, target - nums[i]);\n                for(List<Integer> temp : tempLists){\n                    temp.add(0,nums[i]);\n                    res.add(temp);\n                }\n            }\n            return res;\n        }\n    }\n```
[]