Leetcode018 4Sum
public class S018 {
    //借鉴S015,速度有些慢
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i =0;i<nums.length-3;i++){
            if(i>0&&nums[i]==nums[i-1]){
                continue;//重复的直接跳过
            }
            for(int j = i+1;j<nums.length-2;j++){
                if(j>i+1&&nums[j]==nums[j-1]){
                    continue;//重复的直接跳过
                }
                int left = j+1;//从i+1开始也是防止重复的办法
                int right = nums.length-1;
                while(left<right){
                    if(nums[left]+nums[right]+nums[i]+nums[j] == target){
                        List<Integer> temp = new ArrayList<Integer>();//必须每次新建
                        temp.add(nums[i]);
                        temp.add(nums[left]);
                        temp.add(nums[right]);
                        temp.add(nums[j]);
                        Collections.sort(temp);
                        result.add(temp);
                        //特别注意下面两个while循环
                        left++;
                        right--;//防止重复
                        while(left<right&&nums[left]==nums[left-1]){
                            left++;//防止重复
                        }
                        while(left<right&&nums[right]==nums[right+1]){
                            right--;//防止重复
                        }
                        //这两个条件特别重要,思考一下为何分别是left++和right--;
                    }else if(nums[left]+nums[right]+nums[i]+nums[j]<target){
                        left++;
                    }else{
                        right--;
                    }
                }
            }
        }
        return result;
    }
}
Leetcode018 4Sum的更多相关文章
- [LeetCode] 4Sum II 四数之和之二
		
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
 - [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 = tar ...
 - LeetCode:3Sum, 3Sum Closest, 4Sum
		
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
 - 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum
		
18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...
 - No.018:4Sum
		
问题: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
 - 6.3Sum  && 4Sum  [ && K sum ] && 3Sum Closest
		
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
 - 3Sum & 4Sum
		
3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...
 - 【leetcode】4Sum
		
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
 - 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum
		
2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...
 
随机推荐
- Python基础知识学习_Day5
			
一.生成器和迭代器 1.列表生成 >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a = map(lambda x:x+1, a ...
 - Beego学习笔记——Config
			
配置文件解析 这是一个用来解析文件的库,它的设计思路来自于database/sql,目前支持解析的文件格式有ini.json.xml.yaml,可以通过如下方式进行安装: go get github. ...
 - openstack私有云布署实践【19 通过python客户端 创建实例VM指定IP地址】
			
还有一种创建方式 是使用py开发工具,调用openstackclient的方法进行创建实例 ,好处就是可随意指定我们要的虚拟机IP地址,需求的场景就是,某天我们需要主动分配一个比较熟知的IP用作某个服 ...
 - FZU 2243 Daxia like uber
			
枚举,最短路. 求出5个点出发的最短路,然后枚举一下这些点之间走的顺序. #pragma comment(linker, "/STACK:1024000000,1024000000" ...
 - Python学习笔记——基础篇【第六周】——shutil模块
			
常用模块之shutil 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,可以部分内容 def c ...
 - 递归——CPS(二)
			
给出一个计算树深度的函数: function treeDepth(curtree) { if(curtree == null) return 0; else { var leftDepth = tre ...
 - 容易忘记的几个Linux命令
			
#查看文件或者目录的属性ls -ld filenamels -ld directory #vi编辑器输入:.,$d #清除全部内容 #修改管理员.用户密码passwd user #("use ...
 - jsp或servlet返回并刷新页面
			
2012-04-27 22:39:05| 分类: JAVA | 标签:返回刷新 |举报|字号 订阅 只用window.history.back(-1);返回页面但不会刷新,还是原来的数据 ...
 - centos6.5 安装ansible,管理多台服务器
			
安装python(最低2.6v) (1).python2.7安装 wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz # tar ...
 - hadoop操作
			
常用命令: https://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html ls命令 /usr/bin/hadoop/software/hadoop/ ...