zoukankan      html  css  js  c++  java
  • 18. 4Sum

    public class Solution {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> ret=new ArrayList<List<Integer>>();
            Arrays.sort(nums);
            
            for(int i=0;i<nums.length;i++)
                if(i==0||nums[i]!=nums[i-1])
                    for(int j=i+1;j<nums.length;j++)
                        if(j==i+1||nums[j]!=nums[j-1])
                        {
                            int l=j+1;
                            int r=nums.length-1;
                            while(l<r)
                            {
                                int sum=nums[i]+nums[j]+nums[l]+nums[r];
                                if(sum==target)
                                {
                                    ret.add(Arrays.asList(new Integer[]{nums[i],nums[j],nums[l],nums[r]}));
                                    while(l<r&&nums[l]==nums[l+1])l++;
                                    while(l<r&&nums[r]==nums[r-1])r--;
                                    l++;
                                    r--;
                                }
                                else if (sum<target)
                                    l++;
                                else
                                    r--;
                            }
                        }
            
            return ret;
        }
    }
  • 相关阅读:
    python
    python
    python
    python
    python 序列化
    字典
    异常处理
    类的成员,类的特殊方法
    HTMLEditor类常用方法说明
    HTMLEditor类常用属性说明
  • 原文地址:https://www.cnblogs.com/asuran/p/7578885.html
Copyright © 2011-2022 走看看