Leetcode-18 4Sum

Source: Internet
Author: User



Problem Description: Givenan ArrayS of n integers, is there elements a, b, C, and D i n 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 is in non-descending order. (ie,abcd)

  • The solution set must not Containduplicate quadruplets.

For example, given array S = {1 0-1 0-22}, and target = 0.

A Solution set is:

( -1, 0, 0, 1)

(-2,-1, 1, 2)

( -2, 0, 0, 2)

Problem Analysis:ksum problem, turn down to the basic 2Sum problem, and pay attention to eliminate duplicate value

Code:

Java Solution:

public class Solution {public list<list<integer>> foursum (int[] num, int target) {list<list< integer>> result = new arraylist<list<integer>> (); if (num = = NULL | | num.length <= 3) return result;// The array is sorted first arrays.sort (num), int temp_target = 0;//The target value of each conversion to 2Sum problem for (int i = 0; i < num.length-3; i++) {//de-duplicated value while ((I ! = 0) && (i < num.length-3) && (num[i] = = num[i-1)) + + i;//after eliminating duplicates, be sure to look at the bounds of the array to avoid any cross-border situations if (I < Nu m.length-3) {for (int j = i + 1, J < Num.length-2; J + +) {while ((j! = i + 1) && (J < num.length-2) &AMP;&A mp (Num[j] = = Num[j-1])) + + j;if (J < num.length-2) {temp_target = Target-num[i]-Num[j];int start = j + 1;int end = Num.length-1;while (STA RT < end) {int temp_sum = Num[start] + num[end]; if (Temp_sum < Temp_target) {+ + start;} else if (Temp_sum > Temp_target) {--end;} else{list<integer> List = new arraylist<> (); List.add (Num[i]); List.add (Num[j]); List.add (Num[start+ +]); List.add (num[end--]); Result.add (list);//continue to search forward and eliminate the same value while (Start < end) && (Num[start] = = Num[start -1]) + + start;while ((Start < end) && (num[end] = = num[end + 1]))--End;}}}}}    return result; }}
Class Solution {public:    vector<vector<int> > Foursum (vector<int> &num, int target) {        // Note:the Solution Object is instantiated only once.        vector<vector<int>> Res;    int numlen = Num.size (), if (Num.size () <4) return Res;sort (Num.begin (), Num.end ());set<vector<int>> tmpres;for (int i = 0; i < Numlen; i++) {for (int j = i+1; J < Numlen; J + +) {int begin = J+1;int end = Numlen-1;while (BE Gin < end) {int sum = num[i]+ Num[j] + num[begin] + num[end];if (sum = = target) {vector<int> tmp;tmp.push_back (num[i ]); Tmp.push_back (Num[j]); Tmp.push_back (Num[begin]); Tmp.push_back (Num[end]); Tmpres.insert (TMP); begin++;end--;} else if (sum<target) begin++;elseend--;}}} Set<vector<int>>::iterator it = Tmpres.begin (); for (; It! = Tmpres.end (); it++) Res.push_back (*it); return res;    };




Leetcode-18 4Sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.