018四数加算


LeetCode第18題四数加算
n個の整数を含む配列Sが与えられ、数列Sに要素a,b,c,dがa+b+c+d=targetとなるかどうか.配列内で、各要素の加算が特定の値に等しいすべての重複しない組合せを見つけてください.注:ソリューションセットには、重複する4元の組合せは含まれません.例えば、所与の配列S=[1,0,−1,0,−2,2]が与えられ、target=0が与えられる.
例文の答えは、[-1,0,0,1],[-2,-1,1,2],[-2,0,0,2]]実はこの問題と15番目の問題の3数の和は類似しており、3数の和の解題ステップに加えてループが追加され、今回の問題の4数加算となっている.同時に、時間を節約するために、私たちはまたプログラムにいくつかの条件判断を追加して、条件を満たさなければ、恐れのない演算をしていません.具体的にはコードを参照してください.
Java

    public static List> fourSum(int[] nums, int target) {
        List> result = new LinkedList<>();
        Arrays.sort(nums);
        if (nums.length < 4)
            return result;
        int n = nums.length;
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;///
            if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target)
                break;///      ,    ,    
            if (nums[i] + nums[n - 1] + nums[n - 2] + nums[n - 3] < target)
                continue;///
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i+1 && nums[j] == nums[j - 1])
                    continue;///   
                if ((j + 2) < n && nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target)
                    break;
                if (nums[i] + nums[j] + nums[n - 1] + nums[n - 2] < target)
                    continue;
                int left = j + 1, right = n - 1;
                while (left < right) {
                    int current_sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (current_sum == target) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left + 1])
                            left++;
                        while (left < right && nums[right] == nums[right - 1])
                            right--;
                        left++;
                        right--;
                    } else if (current_sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }
        return result;
    }

C++

class Solution
{
public:
    vector<vector<int> > fourSum(vector<int>& nums,int target)
    {
        vector<vector<int> > result=vector<vector<int> >();
        if(nums.size()<4)return result;
        sort(nums.begin(),nums.end());
        int n = nums.size();
        for(int i=0; i3; i++)
        {
            if(i>0&&nums[i]==nums[i-1])continue;
            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target)break;
            if(nums[i]+nums[n-3]+nums[n-2]+nums[n-1]continue;
            for(int j=i+1; j2; j++)
            {
                if(j>i+1&&nums[j]==nums[j-1])continue;///   j>j+1   j>0            
                if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target)break;
                if(nums[i]+nums[j]+nums[n-2]+nums[n-1]continue;
                int low=j+1,high=n-1;
                while(lowint current_sum=nums[i]+nums[j]+nums[low]+nums[high];
                    if(current_sum==target)
                    {
                        vector<int>vec=vector<int>();
                        vec.push_back(nums[i]);
                        vec.push_back(nums[j]);
                        vec.push_back(nums[low]);
                        vec.push_back(nums[high]);
                        result.push_back(vec);
                        while(low1])low++;
                        while(low1])high--;
                        low++;
                        high--;
                    }
                    else if(current_sumelse
                        high--;
                }
            }
        }


        return result;
    }
};

Python
class Solution(object):
    def fourSum(self, nums, target):
        result = []
        if len(nums) < 4:
            return result
        nums = sorted(nums)
        n = len(nums)
        for i in range(n - 3):
            if (i > 0 and nums[i] == nums[i - 1]): continue
            if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target): break
            if (nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target): continue
            for j in range(i + 1, n - 2):
                if (j > i + 1 and nums[j] == nums[j - 1]): continue
                if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target): break
                if (nums[i] + nums[j] + nums[n - 2] + nums[n - 1] < target): continue
                low, high = j + 1, n - 1
                while (low < high):
                    current_sum = nums[i] + nums[j] + nums[low] + nums[high]
                    if (current_sum == target):
                        result.append([nums[i], nums[j], nums[low], nums[high]])
                        while (low < high and nums[low] == nums[low + 1]): low = low + 1
                        while (low < high and nums[high] == nums[high - 1]): high = high - 1
                        low = low + 1
                        high = high - 1
                    elif current_sum < target:
                        low = low + 1
                    else:
                        high = high - 1
        return result