47. Permutations II

LeetCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> output =new LinkedList();
List<Integer> nums_lst=new ArrayList<Integer>();

for(int num:nums) nums_lst.add(num);
backtrack(nums.length,nums_lst,output,0);
return output;
}

static void backtrack(int n, List<Integer> nums_lst, List<List<Integer>> output, int first){
if (first==n) {
if(!output.contains(nums_lst))output.add(new ArrayList<Integer>(nums_lst));
}
for (int i = first; i < n; i++) {
Collections.swap(nums_lst, first, i);
backtrack(n, nums_lst, output, first + 1);
Collections.swap(nums_lst, first, i);
}
}
}
0%