8000 refactor 582 · hemanthsavasere/Leetcode@afd6cb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit afd6cb2

Browse files
refactor 582
1 parent c3b120c commit afd6cb2

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/main/java/com/fishercoder/solutions/_582.java

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

3-
43
import java.util.ArrayDeque;
54
import java.util.Deque;
65
import java.util.HashMap;
@@ -41,24 +40,26 @@
4140
*/
4241
public class _582 {
4342

44-
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
45-
Map<Integer, List<Integer>> map = new HashMap<>();
46-
for (int i = 0; i < pid.size(); i++) {
47-
map.putIfAbsent(ppid.get(i), new LinkedList<>());
48-
map.get(ppid.get(i)).add(pid.get(i));
49-
}
50-
List<Integer> result = new LinkedList<>();
51-
Deque<Integer> stack = new ArrayDeque<>();
52-
stack.offer(kill);
53-
while (!stack.isEmpty()) {
54-
int curr = stack.poll();
55-
result.add(curr);
56-
List<Integer> list = map.get(curr);
57-
if (list != null) {
58-
stack.addAll(list);
43+
public static class Solution1 {
44+
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
45+
Map<Integer, List<Integer>> map = new HashMap<>();
46+
for (int i = 0; i < pid.size(); i++) {
47+
map.putIfAbsent(ppid.get(i), new LinkedList<>());
48+
map.get(ppid.get(i)).add(pid.get(i));
49+
}
50+
List<Integer> result = new LinkedList<>();
51+
Deque<Integer> stack = new ArrayDeque<>();
52+
stack.offer(kill);
53+
while (!stack.isEmpty()) {
54+
int curr = stack.poll();
55+
result.add(curr);
56+
List<Integer> list = map.get(curr);
57+
if (list != null) {
58+
stack.addAll(list);
59+
}
5960
}
61+
return result;
6062
}
61-
return result;
6263
}
6364

6465
}

src/test/java/com/fishercoder/_582Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
* Created by fishercoder on 5/18/17.
1414
*/
1515
public class _582Test {
16-
private static _582 test;
16+
private static _582.Solution1 solution1;
1717
private static List<Integer> pid;
1818
private static List<Integer> ppid;
1919
private static List<Integer> expected;
2020
private static Integer kill;
2121

2222
@BeforeClass
2323
public static void setup() {
24-
test = new _582();
24+
solution1 = new _582.Solution1();
2525
}
2626

2727
@Test
@@ -30,7 +30,7 @@ public void test1() {
3030
ppid = Arrays.asList(3, 0, 5, 3);
3131
kill = 5;
3232
expected = Arrays.asList(5, 10);
33-
assertEquals(expected, test.killProcess(pid, ppid, kill));
33+
assertEquals(expected, solution1.killProcess(pid, ppid, kill));
3434
}
3535

3636
@Test
@@ -39,6 +39,6 @@ public void test2() {
3939
ppid = Arrays.asList(3, 0, 5, 3);
4040
kill = 3;
4141
expected = Arrays.asList(3, 1, 5, 10);
42-
assertEquals(expected, test.killProcess(pid, ppid, kill));
42+
assertEquals(expected, solution1.killProcess(pid, ppid, kill));
4343
}
4444
}

0 commit comments

Comments
 (0)
0