Skip to content

Commit 5d95766

Browse files
refactor 491
1 parent d15b618 commit 5d95766

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.ArrayList;
54
import java.util.HashSet;
65
import java.util.List;
@@ -23,28 +22,29 @@
2322
*/
2423
public class _491 {
2524

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));
3033
}
31-
Set<List<Integer>> answer = new HashSet<>();
32-
List<Integer> list = new ArrayList<>();
33-
return new ArrayList<>(backtracking(nums, 0, list, answer));
34-
}
3534

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+
}
4546
}
47+
return answer;
4648
}
47-
return answer;
4849
}
49-
5050
}

src/test/java/com/fishercoder/_491Test.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
import java.util.List;
99

10-
/**
11-
* Created by stevesun on 5/30/17.
12-
*/
1310
public class _491Test {
14-
private static _491 test;
15-
private static int[] nums;
11+
private static _491.Solution1 solution1;
12+
private static int[] nums;
1613

17-
@BeforeClass
18-
public static void setup() {
19-
test = new _491();
20-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _491.Solution1();
17+
}
2118

22-
@Test
23-
public void test1() {
24-
nums = new int[]{4, 6, 7, 7};
25-
List<List<Integer>> actual = test.findSubsequences(nums);
26-
CommonUtils.printListList(actual);
27-
}
19+
@Test
20+
public void test1() {
21+
nums = new int[] {4, 6, 7, 7};
22+
List<List<Integer>> actual = solution1.findSubsequences(nums);
23+
CommonUtils.printListList(actual);
24+
}
2825
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy