public List<List<Integer>> fourSum(int[] num, int target) {

    Arrays.sort(num);
    List<List<Integer>> results = new ArrayList<List<Integer>>();
    Set<List<Integer>> set = new HashSet<List<Integer>>();

    for (int i = 0 ; i < num.length ; i++) {
      for (int j = i + 1 ; j < num.length ; j++) {

        int head = j + 1;
        int tail = num.length - 1;
        while (head < tail) {
          int sum = num[i] + num[j] + num[head] + num[tail];
          if (sum > target) {
            tail--;
          } else if (sum < target) {
            head++;
          } else { // sum == target
            List<Integer> temp = new ArrayList<Integer>();
            temp.add(num[i]);
            temp.add(num[j]);
            temp.add(num[head]);
            temp.add(num[tail]);

            if (!set.contains(temp)) {
              set.add(temp);
              results.add(temp);
            }
          }

          head++;
          tail--;
        }
      }
    }

    return results;
  }