1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class Solution { List<List<Integer>> result;
public List<List<Integer>> findSubsequences(int[] nums) { result = new ArrayList<List<Integer>>(); LinkedList<Integer> list = new LinkedList<>(); dfs(nums, -1, list); return result; }
private void dfs(int[] nums, int index, LinkedList<Integer> list) { if (list.size() > 1) { result.add(new LinkedList(list)); } HashSet<Integer> set = new HashSet<>(); for (int i = index + 1; i < nums.length; i++) { int temp = nums[i]; if (set.contains(temp)) { continue; } set.add(temp); if (list.isEmpty() || list.getLast() <= temp) { list.addLast(temp); dfs(nums, i, list); list.removeLast(); } } } }
|