public List<List<Integer>> threeSumB(int[] num){
  List<List<Integer>> res=new ArrayList<>();
  Arrays.sort(num);
  for (int i=0; i < num.length - 2; i++) {
    if (num[i] > 0) {
      break;
    }
    if (i == 0 || (i > 0 && num[i] != num[i - 1])) {
      int lo=i + 1, hi=num.length - 1, sum=0 - num[i];
      while (lo < hi) {
        if (num[lo] + num[hi] == sum) {
          res.add(Arrays.asList(num[i],num[lo],num[hi]));
          while (lo < hi && num[lo] == num[lo + 1])           lo++;
          while (lo < hi && num[hi] == num[hi - 1])           hi--;
          lo++;
          hi--;
        }
 else         if (num[lo] + num[hi] < sum) {
          while (lo < hi && num[lo] == num[lo + 1])           lo++;
          lo++;
        }
 else {
          while (lo < hi && num[hi] == num[hi - 1])           hi--;
          hi--;
        }
      }
    }
  }
  return res;
}

public List < List < Integer > > threeSumB ( int [ ] num ) { List < List < Integer > > res = new ArrayList < > ( ) ; Arrays . sort ( num ) ; for ( int i = 0 ; i < num . length - 2 ; i ++ ) { if ( num [ i ] > 0 ) { break ; } if ( i == 0 || ( i > 0 && num [ i ] != num [ i - 1 ] ) ) { int lo = i + 1 , hi = num . length - 1 , sum = 0 - num [ i ] ; while ( lo < hi ) { if ( num [ lo ] + num [ hi ] == sum ) { res . add ( Arrays . asList ( num [ i ] , num [ lo ] , num [ hi ] ) ) ; while ( lo < hi && num [ lo ] == num [ lo + 1 ] ) lo ++ ; while ( lo < hi && num [ hi ] == num [ hi - 1 ] ) hi -- ; lo ++ ; hi -- ; } else if ( num [ lo ] + num [ hi ] < sum ) { while ( lo < hi && num [ lo ] == num [ lo + 1 ] ) lo ++ ; lo ++ ; } else { while ( lo < hi && num [ hi ] == num [ hi - 1 ] ) hi -- ; hi -- ; } } } } return res ; }
more concise . compare first , then move pointer to skip all duplicates . note that it compares current position with the next position . so one more move is needed after that .
