zoukankan      html  css  js  c++  java
  • LeetCode: 4sum

    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, abcd)
    • 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)

     Solution:

    public class Solution {
    public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
    	Arrays.sort(num);
     
    	HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
    	ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
     
    	for (int i = 0; i < num.length; i++) {
    		for (int j = i + 1; j < num.length; j++) {
    			int k = j + 1;
    			int l = num.length - 1;
     
    			while (k < l) {
    				int sum = num[i] + num[j] + num[k] + num[l];
     
    				if (sum > target) {
    					l--;
    				} else if (sum < target) {
    					k++;
    				} else if (sum == target) {
    					ArrayList<Integer> temp = new ArrayList<Integer>();
    					temp.add(num[i]);
    					temp.add(num[j]);
    					temp.add(num[k]);
    					temp.add(num[l]);
     
    					if (!hashSet.contains(temp)) {
    						hashSet.add(temp);
    						result.add(temp);
    					}
     
    					k++;
    					l--;
    				}
    			}
    		}
    	}
     
    	return result;
    }
    
    }
    

    C++

    class Solution {
    public:
        vector<vector<int> > fourSum(vector<int> &num, int target) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            vector<int> tmp;
            vector<vector<int>> res;
            if(num.empty()) return res;
            sort(num.begin(), num.end());
            for(int i=0; i<num.size(); i++)
            {
                int cur = target - num[i];
                for(int j=i+1; j<num.size(); j++)
                {
                    int temp = cur - num[j];
                    int start = j+1, end = num.size()-1;
                    while(start<end)
                    {
                        if(num[start]+num[end]==temp)
                        {
                            tmp.push_back(num[i]);
                            tmp.push_back(num[j]);
                            tmp.push_back(num[start]);
                            tmp.push_back(num[end]);
                            res.push_back(tmp);
                            tmp.clear();
                            start++;
                            end--;
                            while(start<end&&num[start]==num[start-1]) start++;
                            while(start<end&&num[end]==num[end+1]) end--;
                        }
                        else if(num[start]+num[end]<temp)
                        {
                            start++;
                            while(start<end&&num[start]==num[start-1]) start++;
                        }
                        else
                        {
                            end--;
                            while(start<end&&num[end]==num[end+1]) end--;
                        }
                    }
                    while(j<num.size()&&num[j]==num[j+1]) j++; 
                }
                while(i<num.size()&&num[i]==num[i+1]) i++;
            }
            return res;
        }
    };
    

      

  • 相关阅读:
    成立移动互联网公司???
    C++的子对象
    单链表 操作的18种算法
    再论虚函数
    多线程(三)
    多线程(二)
    多线程(一)
    存储过程的参数
    git
    多态(三)
  • 原文地址:https://www.cnblogs.com/yeek/p/3933701.html
Copyright © 2011-2022 走看看