Skip to content

Commit b568535

Browse files
authored
Create Permutations
1 parent 5d1df05 commit b568535

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Permutations

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
List<List<Integer>> res = new ArrayList<>();
3+
public List<List<Integer>> permute(int[] nums) {
4+
dfs(nums, new ArrayList<>(), new HashSet<>());
5+
return res;
6+
}
7+
public void dfs(int[] nums, List<Integer> store, Set<Integer> used){
8+
if(store.size() == nums.length){
9+
res.add(new ArrayList<Integer>(store));
10+
return ;
11+
}
12+
for(int i = 0; i<nums.length; i++){
13+
if(used.contains(nums[i])) continue;
14+
used.add(nums[i]);
15+
store.add(nums[i]);
16+
dfs(nums, store, used);
17+
store.remove(store.size()-1);
18+
used.remove(nums[i]);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)