[LeetCode-interview algorithm classic-Java implementation] [018-4Sum (sum of four numbers)], leetcode -- java
[018-4Sum (sum of four numbers )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Given an array S of n integers, are there elements a, B, c, and d in S such that a + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a, B, c, d) must be in non-descending order. (ie, a ≤ B ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
Theme
Given an integer array, find the Unique Solution of a + B + c + d = target.
Solutions
First, determine the two numbers of a and d. For the numbers of a and d, they cannot be reused at the same time. Then confirm B and c. The two numbers cannot be reused at the same time. Find all solutions that meet the conditions and ensure that the solutions are not repeated.
Code Implementation
Import java. util. arrayList; import java. util. arrays; import java. util. using list; import java. util. list; public class Solution {public List <Integer> fourSum (int [] num, int target) {List <Integer> result = new partition List <> (); if (num = null | num. length <4) {return result;} Arrays. sort (num); // sort the array for (int I = 0; I <num. length-3; I ++) {// the first addition if (I> 0 & num [I] = num [I-1]) {// use the first addition without repeating continue;} for (int j = num. length-1; j> I + 2; j --) {// The fourth addition if (j <num. length-1 & num [j] = num [j + 1]) {// use the fourth addition without repeating continue;} int start = I + 1; // The second addition int end = j-1; // The third addition int n = target-num [I]-num [j]; while (start <end) {if (num [start] + num [end] = n) {List <Integer> four = new ArrayList <> (4); four. add (num [I]); four. add (num [start]); four. add (num [end]); four. add (num [j]); result. add (four); do {start ++;} while (start <end & num [start] = num [start-1]); // ensure that the second number is used again without repeating do {end --;} while (start <end & num [end] = num [end + 1]); // ensure that the third number is used again without duplicates} else if (num [start] + num [end] <n) {do {start ++ ;} while (start <end & num [start] = num [start-1]); // ensure that the second number is retained again} else {do {end --;} while (start <end & num [end] = num [end + 1]); // ensure that the third number is retained. }}} return result ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46997233]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.