[LeetCode] 18. ☆☆☆ four and the number of (the double pointer)

Four and the number of

description

Given a n array of integers and a target nums target, if there are four elements a, b, c, and d nums determined such that a + b + c + d is equal to the value of the target? Identify all satisfy the conditions of the quad and do not repeat.

note:

The answer can not contain duplicate quad.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and the target = 0.

Meet the requirements set for the four-tuple:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

Resolve

And the like, and only the number of three, but the number becomes four. Multi-layer circulation.

First sort, then compare. It may be noted that the minimum and maximum can be determined together, faster cycle.

Code

static List<List<Integer>> resList = new ArrayList<>();
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        if (null == nums || nums.length < 4) {
            return resList;
        }
        Arrays.sort(nums);
        int length = nums.length;
        for (int i = 0; i <= nums.length - 4; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {//相同的值不用再计算
                continue;
            }
             ;int minVal the nums = [I] + the nums [I +. 1] + the nums [I + 2] + the nums [I +. 3 ];
             IF (minVal> target) { // the minimum value is greater than the target, the direct return 
                BREAK ; 
            } 
            int maxVal the nums = [I] + the nums [length -. 1] + the nums [length -2] the nums + [length -. 3 ];
             IF (maxVal <target) { // less than the maximum target, the next cycle 
                Continue ; 
            } 
            for ( int . 1 = I + K; K <= nums.length -. 3; K ++ ) {
                 IF (K> I +. 1 && the nums [K] == the nums [K -. 1]) { // the same value no recalculation 
                    Continue 
                }
                minVal = the nums [I] the nums + [K] + the nums [K +. 1] + the nums [K + 2 ];
                 IF (minVal> target) { // the minimum value is greater than the target, the direct return 
                    BREAK ; 
                } 
                maxVal = the nums [I ] + the nums [K] + the nums [length -. 1] + the nums [length -2 ];
                 IF (maxVal <target) { // less than the maximum target, the next cycle 
                    Continue ; 
                } 
                int Start = K +. 1 ;
                 int End nums.length = -. 1 ;
                 the while (Start < End) {
                     intthe nums = nowRes [I] the nums + [K] + the nums [Start] + the nums [End];
                     IF (target == nowRes) { 
                        resList.add (Arrays.asList (the nums [I], the nums [K], the nums [ Start], the nums [End])); 
                        Start ++ ;
                         the while (Start <End && the nums [Start] == the nums [Start -. 1]) { // the same value no recalculation 
                            Start ++ ; 
                        } 
                        End - ;
                         IF (Start <End && the nums [End] == the nums [End +. 1]) { // the same value no recalculation 
                            end-- ;
                        }
                    } else if (target > nowRes) {
                        start++;
                    } else {
                        end--;
                    }
                }
            }
        }
        return resList;
    }

 

Guess you like

Origin www.cnblogs.com/fanguangdexiaoyuer/p/12160664.html