16.3Sum Closest

134 篇文章 0 订阅
49 篇文章 0 订阅

题目链接:3sum-closest


import java.util.Arrays;


/**
 * 
 * Given an array S of n integers, find three integers in S 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).
 *
 */

public class ThreeSumClosest {

	//超时
    static int threeSumClosest(int[] num, int target) {
    	
    	Arrays.sort(num);
    	int left = 0;
    	int right = num.length - 1;
    	int minGap = Integer.MAX_VALUE;
    	int closest = 0;
    	while(right >= 2 ) {
    		while(left < right - 1) {
    			for (int mid = left + 1; mid < right; mid++) {
					int gap =Math.abs(num[left] + num[mid] + num[right] - target) ;
					
					if(gap < minGap) {
						closest = num[left] + num[mid] + num[right];
						minGap = gap;
					}
				}
    			
    			int preLeft = num[left];
    			while(left < right - 1 && num[left] == preLeft) left++;
    		}
    		
    		left = 0;
    		int preRight = num[right];
    		while(right >= 2 && num[right] == preRight)	right --;
    	}  	
    	return closest;
    }
    
    //超时
    static int threeSumClosest1(int[] num, int target) {
    	
    	Arrays.sort(num);
    	int left = 0;
    	int right = num.length - 1;
    	int minGap = Integer.MAX_VALUE;
    	int closest = 0;
    	while(right >= 2 ) {
    		while(left < right - 1) {
    			boolean isFirst = true;
    			int preGap = 0;
    			for (int mid = left + 1; mid < right; mid++) {
					int gap =Math.abs(num[left] + num[mid] + num[right] - target) ;
					
					if(isFirst) {
						preGap = gap;
						isFirst = false;
					}
					
					if(gap > preGap) break;
					
					if(gap < minGap) {
						closest = num[left] + num[mid] + num[right];
						minGap = gap;
					}
				}
    			
    			int preLeft = num[left];
    			while(left < right - 1 && num[left] == preLeft) left++;
    		}
    		
    		left = 0;
    		int preRight = num[right];
    		while(right >= 2 && num[right] == preRight)	right --;
    	}  	
    	return closest;
    }
    
    // accepted
    static int threeSumClosest2(int[] num, int target) {
    
//    	120 / 120 test cases passed.
//    	Status: Accepted
//    	Runtime: 278 ms
//    	Submitted: 0 minutes ago

    	Arrays.sort(num);
    	int minGap = Integer.MAX_VALUE;
    	int closest = 0;
    	for(int left = 0; left < num.length - 2; left ++) {
    		int mid = left + 1;
    		int right = num.length - 1;
    		while(mid < right) {
    			int sum = num[left] + num[mid] + num[right];
    			if(Math.abs(sum - target) < minGap) {
    				closest = sum;
    				minGap = Math.abs(sum - target);
    			}
    			if(sum < target) mid++;
    			else right --;
    		}
    	}  
    	
    	return closest;
    }
	public static void main(String[] args) {
		System.out.println(threeSumClosest2(new int[]{43,75,-90,47,-49,72,17,-31,-68,-22,-21,-30,65,88,-75,23,97,
				-61,53,87,-3,33,20,51,-79,43,80,-9,34,-89,-7,93,43,55,-94,29,-32,-49,25,72,-6,35,53,63,6,-62,-96,
				-83,-73,66,-11,96,-90,-27,78,-51,79,35,-63,85,-82,-15,100,-82,1,-4,-41,-21,11,12,12,72,-82,-22,37,
				47,-18,61,60,55,22,-6,26,-60,-42,-92,68,45,-1,-26,5,-56,-1,73,92,-55,-20,-43,-56,-15,7,52,35,-90,63,
				41,-55,-58,46,-84,-92,17,-66,-23,96,-19,-44,77,67,-47,-48,99,51,-25,19,0,-13,-88,-10,-67,14,7,89,-69,
				-83,86,-70,-66,-38,-50,66,0,-67,-91,-65,83,42,70,-6,52,-21,-86,-87,-44,8,49,-76,86,-3,87,-32,81,-58,
				37,-55,19,-26,66,-89,-70,-69,37,0,19,-65,38,7,3,1,-96,96,-65,-52,66,5,-3,-87,-16,-96,57,-74,91,46,-79,
				0,-69,55,49,-96,80,83,73,56,22,58,-44,-40,-45,95,99,-97,-22,-33,-92,-51,62,20,70,90, 284}, 1));

	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值