public static int threeSumClosest(int[] num, int target) {
    if(num.length < 3) return 0;
    Arrays.sort(num);
    int s = 0, e = num.length - 1;
    int closes = num[0] + num[1] + num[2];

    while (e > 0) {
        while (s < e) {
            int sum = num[s] + num[e];
            for (int i = s + 1; i < e; i++) {
                if (Math.abs(target -(num[i] + sum)) < Math.abs(target - closes)) {
                  closes = num[i] + sum;
                }
            }
            int same = num[s];
            while(s < e && num[s] == same) s++;
        }
        
        s = 0;
        int same = num[e];
        while(e > 0 && num[e] == same) e--;
    }

    return closes;
  }