15. 16. 18.3Sum 4sum

来源:互联网 发布:淘宝虚假广告怎么处罚 编辑:程序博客网 时间:2024/06/07 03:28

15. 3Sum

Given an array S of n integers, are there elementsa,b,c in S such that a + b +c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.


时间复杂度:O(N^2)

思路: 首先将数组按从小到大的顺序排序; 接着遍历数组,以当前数组的第i个位置作为第一个元素,然后设置2个指针,分别指向当前位置后一个和最后一项,若三者之和为0,则添加,同时第一个指针后移,第二个指针前移; 若大于0,则最后一个指针向前移; 若小于0,则紧挨着元素的指针向后移.

<span style="font-size:14px;">public List<List<Integer>> threeSum(int[] nums) {        List<List<Integer>> list=new ArrayList<List<Integer>>();        if(nums==null||nums.length<3)            return list;        Arrays.sort(nums);                int j,k;        int len=nums.length;        for(int i=0;i<len-2;i++){            j=i+1;            k=len-1;            while(j<k){                if(nums[i]+nums[j]+nums[k]==0){                    List<Integer> temp=new ArrayList<Integer>();                    temp.add(nums[i]);                    temp.add(nums[j]);                    temp.add(nums[k]);                    if(!list.contains(temp))                        list.add(temp);                    j++;                    k--;                }else if(nums[i]+nums[j]+nums[k]<0){                    j++;                }else                    k--;            }        }        return list;    }</span>


16. 3Sum Closest

Given an array S of n integers, find three integers inS such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
时间复杂度:O(N^2)

思路: 和3sum一样

 public int threeSumClosest(int[] nums, int target) {        Arrays.sort(nums);        int len=nums.length;        int j,k;        int result=nums[0]+nums[1]+nums[len-1];        int sum;        for(int i=0;i<len-2;i++){            j=i+1;            k=len-1;            while(j<k){                sum=nums[i]+nums[j]+nums[k];                if(sum==target)                    return sum;                else if(sum<target)                    j++;                else                    k--;                if(Math.abs(sum-target)<Math.abs(result-target))                    result=sum;            }        }        return result;    }


18. 4Sum

Given an array S of n integers, are there elementsa, 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.


时间复杂度:O(N^3)

public List<List<Integer>> fourSum(int[] nums, int target) {        List<List<Integer>> list=new ArrayList<List<Integer>>();        if(nums==null||nums.length<4)            return list;        Arrays.sort(nums);        int len=nums.length;        int p1,p2;        for(int i=0;i<len-3;i++){            if(i>0&&nums[i]==nums[i-1])                continue;            for(int j=i+1;j<len-2;j++){                if((j-i)>1&&nums[j]==nums[j-1])                    continue;                p1=j+1;                p2=len-1;                while(p1<p2){                    if(nums[i]+nums[j]+nums[p1]+nums[p2]==target){                        List<Integer> temp=new ArrayList<Integer>();                        temp.add(nums[i]);                        temp.add(nums[j]);                        temp.add(nums[p1]);                        temp.add(nums[p2]);                        if(!list.contains(temp))                            list.add(temp);                        p1++;                        p2--;                    }else if(nums[i]+nums[j]+nums[p1]+nums[p2]<target){                        p1++;                    }else                        p2--;                }            }                        }        return list;    }


0 0
原创粉丝点击