Skip to content

Commit 37b24da

Browse files
refactor 473
1 parent cd096a6 commit 37b24da

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
*/
2828
public class _473 {
2929

30-
/**Partially inspired by: https://discuss.leetcode.com/topic/72107/java-dfs-solution-with-explanation/2*/
31-
32-
/**One hidden requirement: you'll have to use up all of the given matchsticks, nothing could be left behind.*/
30+
public static class Solution1 {
31+
/**
32+
* Partially inspired by: https://discuss.leetcode.com/topic/72107/java-dfs-solution-with-explanation/2
33+
* One hidden requirement: you'll have to use up all of the given matchsticks, nothing could be left behind.
34+
*/
3335
public boolean makesquare(int[] nums) {
3436
if (nums == null || nums.length < 4) {
3537
return false;
@@ -47,35 +49,36 @@ public boolean makesquare(int[] nums) {
4749
return dfs(nums, new int[4], 0, sum / 4);
4850
}
4951

50-
private boolean dfs(int[] nums, int[] sums, int index, int target) {
51-
if (index == nums.length) {
52-
if (sums[0] == target && sums[1] == target && sums[2] == target) {
53-
return true;
54-
}
55-
return false;
56-
}
57-
for (int i = 0; i < 4; i++) {
58-
if (sums[i] + nums[index] > target) {
59-
continue;
52+
private boolean dfs(int[] nums, int[] sums, int index, int target) {
53+
if (index == nums.length) {
54+
if (sums[0] == target && sums[1] == target && sums[2] == target) {
55+
return true;
56+
}
57+
return false;
6058
}
61-
sums[i] += nums[index];
62-
if (dfs(nums, sums, index + 1, target)) {
63-
return true;
59+
for (int i = 0; i < 4; i++) {
60+
if (sums[i] + nums[index] > target) {
61+
continue;
62+
}
63+
sums[i] += nums[index];
64+
if (dfs(nums, sums, index + 1, target)) {
65+
return true;
66+
}
67+
sums[i] -= nums[index];
6468
}
65-
sums[i] -= nums[index];
69+
return false;
6670
}
67-
return false;
68-
}
6971

70-
private void reverse(int[] nums) {
71-
int left = 0;
72-
int right = nums.length - 1;
73-
while (left < right) {
74-
int tmp = nums[left];
75-
nums[left] = nums[right];
76-
nums[right] = tmp;
77-
left++;
78-
right--;
72+
private void reverse(int[] nums) {
73+
int left = 0;
74+
int right = nums.length - 1;
75+
while (left < right) {
76+
int tmp = nums[left];
77+
nums[left] = nums[right];
78+
nums[right] = tmp;
79+
left++;
80+
right--;
81+
}
7982
}
8083
}
8184

src/test/java/com/fishercoder/_473Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
* Created by stevesun on 5/30/17.
1111
*/
1212
public class _473Test {
13-
private static _473 test;
13+
private static _473.Solution1 solution1;
1414
private static int[] nums;
1515

1616
@BeforeClass
1717
public static void setup() {
18-
test = new _473();
18+
solution1 = new _473.Solution1();
1919
}
2020

2121
@Test
2222
public void test1() {
2323
nums = new int[]{1, 1, 2, 2, 2};
24-
assertEquals(true, test.makesquare(nums));
24+
assertEquals(true, solution1.makesquare(nums));
2525
}
2626
}

0 commit comments

Comments
 (0)