Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

47. Permutations II

Posted on 2020-07-01 | Edited on 2021-01-22

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);
}
}
}
<1…121122123…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%