Title: Force bucklehttps://leetcode-cn.com/problems/4sum/
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(nums); int l; int r; for(int index1=0;index1<nums.length-3;index1++){ if(index1!=0&&nums[index1]==nums[index1-1]){ continue; } for(int index2=index1+1;index2<nums.length-2;index2++){ if(index2!=index1+1&&nums[index2]==nums[index2-1]){ continue; } l = index2+1; r = nums.length-1; while(l<r){ if(nums[index1]+nums[index2]+nums[l]+nums[r]==target){ List<Integer> list = new ArrayList<>(); list.add(nums[index1]); list.add(nums[index2]); list.add(nums[l]); list.add(nums[r]); ans.add(list); while(l<r&&nums[l]==nums[l+1]) l++; while(l<r&&nums[r]==nums[r-1]) r--; l++; r--; }else{ if(nums[index1]+nums[index2]+nums[l]+nums[r]>target){ r--; }else{ l++; } } } } } return ans; } }
Idea: this question and Leetcode 15. Sum of three numbers Very similar. The idea here is similar to that of the sum of three. The idea of the sum of three numbers is to sort first, enumerate the first number, and then use the double pointer method to find the two elements that meet the requirements of the topic; Here, we also sort first, then enumerate the first two elements respectively, and then use the double finger needle method to find the two elements that meet the requirements of the topic.
1. Declare the variable ans of List type, which is the element to be returned; Sort the nums [] array through the method of Array.sotr(); Declare l and r respectively, which are left pointer and right pointer respectively.
List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(nums); int l; int r;
2.index1 is the first element of enumeration, and index2 is the second element of enumeration. In order to avoid repeated solutions caused by enumerating the same elements, it is necessary to judge whether the value of the element referred to in the index is consistent with the value of the element referred to in the previous index. If so, it is necessary to skip. Index also needs to repeat the above judgment. Then assign values to the left pointer l and the right pointer r respectively. The initial value of the left pointer should be the next element of the second element of the enumeration, and the initial value of the right pointer should be the last element of the array.
for(int index1=0;index1<nums.length-3;index1++){ if(index1!=0&&nums[index1]==nums[index1-1]){ continue; } for(int index2=index1+1;index2<nums.length-2;index2++){ if(index2!=index1+1&&nums[index2]==nums[index2-1]){ continue; } l = index2+1; r = nums.length-1; //...... } }
3. If the value of the sum of four numbers is equal to target, store the values of these four elements in the list, and then store the list in ans, and then move the left pointer to the right and the right pointer to the left at the same time (if the value of the element pointed to by the pointer is the same as the value of the previous element, move the pointer again to avoid pointing to duplicate values and causing duplicate solutions). If the value of the sum of four numbers is not equal to target, you need to judge whether the sum of four numbers is greater than or less than target. If the sum of four numbers is greater than target, move the right pointer to the left; If the sum of four numbers is less than target, move the left pointer to the right. Until the left and right pointers coincide, it ends.
while(l<r){ if(nums[index1]+nums[index2]+nums[l]+nums[r]==target){ List<Integer> list = new ArrayList<>(); list.add(nums[index1]); list.add(nums[index2]); list.add(nums[l]); list.add(nums[r]); ans.add(list); while(l<r&&nums[l]==nums[l+1]) l++; while(l<r&&nums[r]==nums[r-1]) r--; l++; r--; }else{ if(nums[index1]+nums[index2]+nums[l]+nums[r]>target){ r--; }else{ l++; } } }
4. After completing the above cycle, ans has saved the answers required for the questions and can return.
return ans;