public int threeSumClosest_2(int[] num, int target) {
        if(num == null || num.length < 3) return Integer.MAX_VALUE;
        Arrays.sort(num);
        int res = num[0]+num[1]+num[2];
        int len = num.length;
        for(int p = 0; p < len; ++p){
            int i = p+1;
            int j = len - 1;
            while (i < j){
                int sum = num[p]+num[i]+num[j];
                if (sum < target) ++i; // all combinations of i and x(x>j) are removed
                else if (sum > target) --j; // all combination of j and x(x<i) are removed
                else return sum;
                if(Math.abs(target-sum) < Math.abs(target-res)) res = sum;
            }
        }
        return res;
    }