Skip to content

Commit fa914b8

Browse files
refactor 356
1 parent 3985a99 commit fa914b8

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Set;
55

66
/**
7+
* 356. Line Reflection
8+
*
79
* Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.
810
911
Example 1:
@@ -22,24 +24,26 @@ Could you do better than O(n2)?
2224
For each point, make sure that it has a reflected point in the opposite side.
2325
*/
2426
public class _356 {
25-
/**credit: https://discuss.leetcode.com/topic/48172/simple-java-hashset-solution*/
26-
public boolean isReflected(int[][] points) {
27-
int max = Integer.MIN_VALUE;
28-
int min = Integer.MAX_VALUE;
29-
Set<String> set = new HashSet<>();
30-
for (int[] point : points) {
31-
max = Math.max(max, point[0]);
32-
min = Math.min(min, point[0]);
33-
String str = point[0] + "a" + point[1];
34-
set.add(str);
35-
}
36-
int sum = max + min;
37-
for (int[] p : points) {
38-
String str = (sum - p[0]) + "a" + p[1];
39-
if (!set.contains(str)) {
40-
return false;
27+
public static class Solution1 {
28+
/** credit: https://discuss.leetcode.com/topic/48172/simple-java-hashset-solution */
29+
public boolean isReflected(int[][] points) {
30+
int max = Integer.MIN_VALUE;
31+
int min = Integer.MAX_VALUE;
32+
Set<String> set = new HashSet<>();
33+
for (int[] point : points) {
34+
max = Math.max(max, point[0]);
35+
min = Math.min(min, point[0]);
36+
String str = point[0] + "a" + point[1];
37+
set.add(str);
38+
}
39+
int sum = max + min;
40+
for (int[] p : points) {
41+
String str = (sum - p[0]) + "a" + p[1];
42+
if (!set.contains(str)) {
43+
return false;
44+
}
4145
}
46+
return true;
4247
}
43-
return true;
4448
}
4549
}

src/test/java/com/fishercoder/_356Test.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/1/17.
11-
*/
129
public class _356Test {
13-
private static _356 test;
14-
private static int[][] points;
10+
private static _356.Solution1 solution1;
11+
private static int[][] points;
1512

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

21-
@Test
22-
public void test1() {
23-
points = new int[][]{
24-
{1, 1},
25-
{-1, 1},
26-
};
27-
assertEquals(true, test.isReflected(points));
28-
}
18+
@Test
19+
public void test1() {
20+
points = new int[][] {
21+
{1, 1},
22+
{-1, 1},
23+
};
24+
assertEquals(true, solution1.isReflected(points));
25+
}
2926

30-
@Test
31-
public void test2() {
32-
points = new int[][]{
33-
{1, 1},
34-
{-1, -1},
35-
};
36-
assertEquals(false, test.isReflected(points));
37-
}
27+
@Test
28+
public void test2() {
29+
points = new int[][] {
30+
{1, 1},
31+
{-1, -1},
32+
};
33+
assertEquals(false, solution1.isReflected(points));
34+
}
3835
}

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