Topic link: LeeCode46 full arrangement
Topic description:
The simplest deep search, as long as there is no pruning, all the matched solutions will be returned. The only pit is that there are negative numbers in the sample. The boolean array I used to judge before will report an error , Just change to map
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
Map<Integer,Boolean> map=new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i],true);
}
for (int i = 0; i < nums.length; i++) {
List<Integer> list=new ArrayList<>();
list.add(nums[i]);
map.put(nums[i],false );
dfs(nums,1,lists,list,map);
map.put(nums[i],true );
}
return lists;
}
public static void dfs(int[] nums,int step,List<List<Integer>> lists,List<Integer> list,Map<Integer,Boolean> map){
if (step >= nums.length) {
List<Integer> list1=new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
list1.add(list.get(i));
}
lists.add(list1);
list.remove(list.size()-1);
return;
}
for (int i = 0; i < nums.length; i++) {
if(map.get(nums[i])==true){
list.add(nums[i]);
map.put(nums[i],false);
dfs(nums, step+1, lists, list, map);
map.put(nums[i],true);
}
}
list.remove(list.size()-1);
}