Here is my code to return all the possible subsets of a set.
Now my question is why when we uncomment the 1. line then the code works fine
But when we uncomment the 2. line it simply returns a empty set
why?
Please help me with the logic behind it.
********************************************Code** ***********************************************
public class Solution {
public ArrayList<ArrayList<Integer>> ans;
public void calc(ArrayList<Integer> A,ArrayList<Integer> arr,int top){
for(int i = top;i<A.size();i++){
arr.add(A.get(i));
ArrayList<Integer> temp = new ArrayList<>();
for(int j = 0;j<arr.size();j++){
temp.add(arr.get(j));
}
if(!ans.contains(temp)){
ans.add(temp);
}
calc(A,arr,i+1);
arr.remove(arr.size()-1);
}
}
public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> A) {
Collections.sort(A);
ans = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
// 1. ans.add(new ArrayList<>());
// 2. ans.add(temp);
calc(A,temp,0);
return ans;
}
}
***************************************code******* *******************************************