3sum

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

注意两点:输出的数字有序,不能重复

思路:

1.首先排序,用快速排序

2.先确定第一个数a,设target = -a。设置左右指针low,high,首尾往中间夹,

如果low+high>target,high--;

如果low+high<target,low++;

如果low+high==target,记录下三个数,add到list里

3.特别要注意,如果第一个就>0,则跳过不再查找,减少循环次数

还有就是去重的问题:

用三个temp记录下target,low,high的值,target与上一次相等,则跳过不再查找;

low和high与上次的数值同时相等,则不add到list里。

下面是代码,快排部分省略

public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
		int size = num.length;
		int right = size-1;
		quickSort(num,0,right);
		
		int temp = -1;
		int temp1 = -1;
		int temp2 = -1;
		ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
		for(int i = 0;i<size;i++){			
			int target = 0-num[i];
			if(target >= 0){	
				if(target != temp){
					temp = target;
					int low = i+1;
					int high = size-1;
					while(low < high){
						int sum = num[low]+num[high];
						if(sum > target){
							high--;
						}
						if(sum < target){
							low++;
						}
						if(sum == target){
							if(!(temp1 == num[low] && temp2 == num[high])){
								ArrayList<Integer> list = new ArrayList<Integer>();
								list.add(num[i]);
								list.add(num[low]);
								list.add(num[high]);
								lists.add(list);
								temp1 = num[low];
								temp2 = num[high];
							}
							high--;
							low++;							
						}
					}
				}
			}
		}
		return lists;
    }

第二次:

思路与上面的一样,把twoSum的部分单独用一个函数写出来。特别注意要去重,设置临时变量,遍历的过程中,相等就跳过。

public List<List<Integer>> threeSum(int[] num) {
        Arrays.sort(num);
        int len=num.length,i;
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(len<3){
            return lists;
        }
        int temp=num[0]+1;
        for(i=0;i<len-2;i++){
           if(temp!=num[i]){        	   
               twoSum(num,i+1,-num[i],lists);
               temp=num[i];
           }
        }
        return lists;
    }
    
    public void twoSum(int[] num,int start,int target,List<List<Integer>> lists) {
        int len=num.length;
        int low =start,high=len-1,t,lv,rv,lt=num[low]+1,rt=num[high]+1;
        List<Integer> list = new ArrayList<Integer>();
        while(low<high){
            lv=num[low];
            rv=num[high];          
            t=lv+rv;
            if(t==target){
            	if(!(lv==lt && rv==rt)){
	                list = new ArrayList<Integer>();
	                list.add(num[start-1]);
	                list.add(lv);
	                list.add(rv);
	                lists.add(list);
	                lt=lv;
	                rt=rv;
            	}
            	low++;
	        high--;
            }else if(t>target){
                high--;
            }else{
                low++;
            }            
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值