My Easy Java Solution


  • 0
    B
    public class Solution {
    private int minSumAbs = 0;
    	private int target = 0;
    	private int res ; 
    	private int []nums;
    	
    	private final static int START_COUNT = 2;
    	private final static int END_COUNT=-1;
    	public int threeSumClosest(int[] nums, int target) {
    		minSumAbs= Integer.MAX_VALUE;
    		this.target = target;
    		this.nums = nums;
    		
    		searchMiniestTreeSum( 0, 0, START_COUNT);
    		return res;
    	}
    	private void searchMiniestTreeSum(int begin,int sum,int count){
    		if (minSumAbs == 0) {
    			return ;
    		}
    		if (count==END_COUNT) {
    			int resultAbs = Math.abs(sum-target);
    			if (resultAbs<minSumAbs) {
    				minSumAbs = resultAbs;
    				res = sum;
    			}
    			return ;
    		}
    		for(int i=begin,end = nums.length-count;i<end;i++){
    			searchMiniestTreeSum(i+1, sum+nums[i],count-1);
    		}
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.