public List> threeSum(int[] nums) {
List> ans = new ArrayList<>();
List combine = new ArrayList<>();
dfs(nums, ans, combine, 0);
return drop_duplicate(ans);
}
private List> drop_duplicate(List> ans) {
List arrs = new LinkedList<>();
for(List list:ans) {
int[] tmp = list.stream().mapToInt(Integer::intValue).toArray();
Arrays.sort(tmp);
boolean flag = false;
for(int [] a: arrs) {
if (Arrays.equals(a, tmp)) flag = true;
}
if(!flag)arrs.add(tmp);
}
// System.out.println(set);
ans = new ArrayList<>();
for(int[] arr: arrs) {
List list = new ArrayList<>();
for(int a:arr) list.add(a);
ans.add(list);
}
return ans;
}
private void dfs(int[] nums, List> ans, List combine, int idx) {
if(combine.size() == 3) {
int sum = 0;
for(Integer a: combine) sum += a;
if(sum == 0) ans.add(new ArrayList<>(combine));
}
if(idx == nums.length) {
return;
}
dfs(nums, ans, combine, idx+1);
combine.add(nums[idx]);
dfs(nums, ans, combine, idx+1);
combine.remove(combine.size()-1);
return;
}