/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/array/SubsetsII.java
46 строк
1 KB
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
// Given a collection of integers that might contain duplicates, nums, return all possible subsets. // Note: The solution set must not contain duplicate subsets. // For example, // If nums = [1,2,2], a solution is: // [ // [2], // [1], // [1,2,2], // [2,2], // [1,2], // [] // ] public class SubsetsII { public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<List<Integer>>(); if(nums.length == 0 || nums == null) { return result; } helper(nums, new ArrayList<Integer>(), 0, result); return result; } public void helper(int[] nums, ArrayList<Integer> current, int index, List<List<Integer>> result) { result.add(current); for(int i = index; i < nums.length; i++) { if(i > index && nums[i] == nums[i - 1]) { continue; } ArrayList<Integer> newCurrent = new ArrayList<Integer>(current); newCurrent.add(nums[i]); helper(nums, newCurrent, i + 1, result); } } }