CODE 86: Permutations II
来源:互联网 发布:程序员平均薪资 编辑:程序博客网 时间:2024/05/01 22:41
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
public ArrayList<ArrayList<Integer>> permute(int[] num) {// Note: The Solution object is instantiated only once and is reused by// each test case.ArrayList<Integer> numbers = new ArrayList<Integer>();for (int n : num) {numbers.add(n);}ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>(dfs(numbers));return results;}Set<ArrayList<Integer>> dfs(ArrayList<Integer> numbers) {if (numbers.size() <= 1) {Set<ArrayList<Integer>> results = new HashSet<ArrayList<Integer>>();ArrayList<Integer> result = new ArrayList<Integer>(numbers);results.add(result);return results;}Set<ArrayList<Integer>> results = new HashSet<ArrayList<Integer>>();ArrayList<Integer> newNumbers = new ArrayList<Integer>(numbers);for (int i = 0; i < numbers.size(); i++) {Integer number = numbers.get(i);newNumbers.remove(number);Set<ArrayList<Integer>> res = dfs(newNumbers);for (ArrayList<Integer> re : res) {re.add(0, number);results.add(re);}newNumbers.add(number);}return results;}
- CODE 86: Permutations II
- [leet code] Permutations II
- leetcode 日经贴,Cpp code -Permutations II
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- IO总结(六)
- s q l i t e 加密 - S Q L C i p h e r
- CCS中调试DM6467高清视频采集(TVP7002输入)
- 黑马程序员--java异常学习笔记
- shell 编程中的 while 语句
- CODE 86: Permutations II
- 【wikioi】1011 数的计算
- linux vmware player 虚拟网络配置
- 非常对象访问常成员函数,导致内部this指针无法访问非常成员函数
- ruby之 周期性任务
- 编译htop命令
- 算法习题25:写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
- Android Application对象
- DEDECMS学习之遍历GET、POST、COOKIE简写