Skip to content

Commit 12be2ff

Browse files
refactor 593
1 parent a3c3e68 commit 12be2ff

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,70 +21,74 @@ A valid square has four equal sides with positive length and four equal angles (
2121
2222
*/
2323
public class _593 {
24-
/**Note: I don't need to use backtracking to find all permutations, this is an overkill.
25-
* This is the most easy one: https://leetcode.com/articles/kill-process-2/#approach-3-checking-every-case-accepted*/
24+
public static class Solution1 {
25+
/**
26+
* Note: I don't need to use backtracking to find all permutations, this is an overkill.
27+
* This is the most easy one: https://leetcode.com/articles/kill-process-2/#approach-3-checking-every-case-accepted
28+
*/
2629

27-
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
28-
List<int[]> input = new ArrayList<>(Arrays.asList(p1, p2, p3, p4));
29-
List<List<int[]>> allPermuations = getAllPermutations(input);
30-
for (List<int[]> eachPermutation : allPermuations) {
31-
if (isValid(eachPermutation)) {
32-
return true;
30+
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
31+
List<int[]> input = new ArrayList<>(Arrays.asList(p1, p2, p3, p4));
32+
List<List<int[]>> allPermuations = getAllPermutations(input);
33+
for (List<int[]> eachPermutation : allPermuations) {
34+
if (isValid(eachPermutation)) {
35+
return true;
36+
}
3337
}
38+
return false;
3439
}
35-
return false;
36-
}
37-
38-
private List<List<int[]>> getAllPermutations(List<int[]> input) {
39-
List<List<int[]>> result = new ArrayList();
40-
List<int[]> init = new ArrayList<>();
41-
result.add(init);
42-
return backTracking(result, input, 0);
43-
}
4440

45-
private List<List<int[]>> backTracking(List<List<int[]>> result, List<int[]> input, int pos) {
46-
if (pos == input.size()) {
47-
return result;
41+
private List<List<int[]>> getAllPermutations(List<int[]> input) {
42+
List<List<int[]>> result = new ArrayList();
43+
List<int[]> init = new ArrayList<>();
44+
result.add(init);
45+
return backTracking(result, input, 0);
4846
}
49-
List<List<int[]>> newResult = new ArrayList<>();
50-
for (List<int[]> eachList : result) {
51-
for (int i = 0; i <= eachList.size(); i++) {
52-
List<int[]> newList = new ArrayList<>(eachList);
53-
newList.add(i, input.get(pos));
54-
newResult.add(newList);
47+
48+
private List<List<int[]>> backTracking(List<List<int[]>> result, List<int[]> input, int pos) {
49+
if (pos == input.size()) {
50+
return result;
51+
}
52+
List<List<int[]>> newResult = new ArrayList<>();
53+
for (List<int[]> eachList : result) {
54+
for (int i = 0; i <= eachList.size(); i++) {
55+
List<int[]> newList = new ArrayList<>(eachList);
56+
newList.add(i, input.get(pos));
57+
newResult.add(newList);
58+
}
5559
}
60+
result = newResult;
61+
return backTracking(result, input, pos + 1);
5662
}
57-
result = newResult;
58-
return backTracking(result, input, pos + 1);
59-
}
6063

61-
private boolean isValid(List<int[]> points) {
62-
int[] p1 = points.get(0);
63-
int[] p2 = points.get(1);
64-
int[] p3 = points.get(2);
65-
int[] p4 = points.get(3);
66-
double distance = (Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
67-
return distance == (Math.pow(p2[0] - p3[0], 2) + Math.pow(p2[1] - p3[1], 2))
68-
&& distance == (Math.pow(p3[0] - p4[0], 2) + Math.pow(p3[1] - p4[1], 2))
69-
&& distance == (Math.pow(p4[0] - p1[0], 2) + Math.pow(p4[1] - p1[1], 2))
70-
&& isRightAngle(p1, p2, p3)
71-
&& noDuplicate(p1, p2, p3, p4);
72-
}
64+
private boolean isValid(List<int[]> points) {
65+
int[] p1 = points.get(0);
66+
int[] p2 = points.get(1);
67+
int[] p3 = points.get(2);
68+
int[] p4 = points.get(3);
69+
double distance = (Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
70+
return distance == (Math.pow(p2[0] - p3[0], 2) + Math.pow(p2[1] - p3[1], 2))
71+
&& distance == (Math.pow(p3[0] - p4[0], 2) + Math.pow(p3[1] - p4[1], 2))
72+
&& distance == (Math.pow(p4[0] - p1[0], 2) + Math.pow(p4[1] - p1[1], 2))
73+
&& isRightAngle(p1, p2, p3)
74+
&& noDuplicate(p1, p2, p3, p4);
75+
}
7376

74-
public boolean noDuplicate(int[] p1, int[] p2, int[] p3, int[] p4) {
75-
return !Arrays.equals(p1, p2)
76-
&& !Arrays.equals(p1, p3)
77-
&& !Arrays.equals(p1, p4)
78-
&& !Arrays.equals(p2, p3)
79-
&& !Arrays.equals(p2, p4)
80-
&& !Arrays.equals(p3, p4);
81-
}
77+
public boolean noDuplicate(int[] p1, int[] p2, int[] p3, int[] p4) {
78+
return !Arrays.equals(p1, p2)
79+
&& !Arrays.equals(p1, p3)
80+
&& !Arrays.equals(p1, p4)
81+
&& !Arrays.equals(p2, p3)
82+
&& !Arrays.equals(p2, p4)
83+
&& !Arrays.equals(p3, p4);
84+
}
8285

83-
public boolean isRightAngle(int[] p1, int[] p2, int[] p3) {
84-
double angle1 = Math.atan2(p2[1] - p1[1], p2[0] - p1[0]);
85-
double angle2 = Math.atan2(p3[1] - p1[1], p3[0] - p1[0]);
86-
double degree = Math.toDegrees(angle1 - angle2);
87-
return degree % 45 == 0;
86+
public boolean isRightAngle(int[] p1, int[] p2, int[] p3) {
87+
double angle1 = Math.atan2(p2[1] - p1[1], p2[0] - p1[0]);
88+
double angle2 = Math.atan2(p3[1] - p1[1], p3[0] - p1[0]);
89+
double degree = Math.toDegrees(angle1 - angle2);
90+
return degree % 45 == 0;
91+
}
8892
}
8993

9094
}

src/test/java/com/fishercoder/_593Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
* Created by fishercoder on 5/22/17.
1111
*/
1212
public class _593Test {
13-
private static _593 test;
13+
private static _593.Solution1 solution1;
1414
private static int[] p1;
1515
private static int[] p2;
1616
private static int[] p3;
1717
private static int[] p4;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _593();
21+
solution1 = new _593.Solution1();
2222
}
2323

2424
@Test
@@ -27,7 +27,7 @@ public void test1() {
2727
p2 = new int[]{1, 1};
2828
p3 = new int[]{1, 0};
2929
p4 = new int[]{0, 1};
30-
assertEquals(true, test.validSquare(p1, p2, p3, p4));
30+
assertEquals(true, solution1.validSquare(p1, p2, p3, p4));
3131
}
3232

3333
@Test
@@ -36,7 +36,7 @@ public void test2() {
3636
p2 = new int[]{5, 3};
3737
p3 = new int[]{3, 5};
3838
p4 = new int[]{7, 7};
39-
assertEquals(false, test.validSquare(p1, p2, p3, p4));
39+
assertEquals(false, solution1.validSquare(p1, p2, p3, p4));
4040
}
4141

4242
@Test
@@ -45,7 +45,7 @@ public void test3() {
4545
p2 = new int[]{0, 0};
4646
p3 = new int[]{0, 0};
4747
p4 = new int[]{0, 0};
48-
System.out.println(test.noDuplicate(p1, p2, p3, p4));
49-
assertEquals(false, test.validSquare(p1, p2, p3, p4));
48+
System.out.println(solution1.noDuplicate(p1, p2, p3, p4));
49+
assertEquals(false, solution1.validSquare(p1, p2, p3, p4));
5050
}
5151
}

0 commit comments

Comments
 (0)