|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.ArrayList;
|
5 | 4 | import java.util.HashSet;
|
6 | 5 | import java.util.List;
|
|
23 | 22 | */
|
24 | 23 | public class _491 {
|
25 | 24 |
|
26 |
| - /**I made it by myself this time! Cheers!*/ |
27 |
| - public List<List<Integer>> findSubsequences(int[] nums) { |
28 |
| - if (nums == null || nums.length == 1) { |
29 |
| - return new ArrayList<>(); |
| 25 | + public static class Solution1 { |
| 26 | + public List<List<Integer>> findSubsequences(int[] nums) { |
| 27 | + if (nums == null || nums.length == 1) { |
| 28 | + return new ArrayList<>(); |
| 29 | + } |
| 30 | + Set<List<Integer>> answer = new HashSet<>(); |
| 31 | + List<Integer> list = new ArrayList<>(); |
| 32 | + return new ArrayList<>(backtracking(nums, 0, list, answer)); |
30 | 33 | }
|
31 |
| - Set<List<Integer>> answer = new HashSet<>(); |
32 |
| - List<Integer> list = new ArrayList<>(); |
33 |
| - return new ArrayList<>(backtracking(nums, 0, list, answer)); |
34 |
| - } |
35 | 34 |
|
36 |
| - private Set<List<Integer>> backtracking(int[] nums, int start, List<Integer> currList, Set<List<Integer>> answer) { |
37 |
| - if (currList.size() >= 2) { |
38 |
| - answer.add(new ArrayList<>(currList)); |
39 |
| - } |
40 |
| - for (int i = start; i < nums.length; i++) { |
41 |
| - if (currList.size() == 0 || currList.get(currList.size() - 1) <= nums[i]) { |
42 |
| - currList.add(nums[i]); |
43 |
| - backtracking(nums, i + 1, currList, answer); |
44 |
| - currList.remove(currList.size() - 1); |
| 35 | + private Set<List<Integer>> backtracking(int[] nums, int start, List<Integer> currList, |
| 36 | + Set<List<Integer>> answer) { |
| 37 | + if (currList.size() >= 2) { |
| 38 | + answer.add(new ArrayList<>(currList)); |
| 39 | + } |
| 40 | + for (int i = start; i < nums.length; i++) { |
| 41 | + if (currList.size() == 0 || currList.get(currList.size() - 1) <= nums[i]) { |
| 42 | + currList.add(nums[i]); |
| 43 | + backtracking(nums, i + 1, currList, answer); |
| 44 | + currList.remove(currList.size() - 1); |
| 45 | + } |
45 | 46 | }
|
| 47 | + return answer; |
46 | 48 | }
|
47 |
| - return answer; |
48 | 49 | }
|
49 |
| - |
50 | 50 | }
|
0 commit comments