Skip to content

Commit 54fc57f

Browse files
committed
Resolved Checkstyle violations
1 parent a4d91c6 commit 54fc57f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,61 @@
1+
package com.thealgorithms.misc;
2+
13
import java.util.ArrayList;
24
import java.util.Arrays;
35
import java.util.List;
46

5-
public class fourSum {
6-
public static List<List<Integer>> fourSum(int[] nums, int target) {
7+
public class FourSum { // Class name changed
8+
// Method name changed to findFourSum
9+
public static List<List<Integer>> findFourSum(int[] nums, int target) {
710
List<List<Integer>> result = new ArrayList<>();
8-
if (nums == null || nums.length < 4) return result;
11+
if (nums == null || nums.length < 4) {
12+
return result; // Added curly braces for 'if'
13+
}
914

1015
Arrays.sort(nums); // Sort the array first
1116

1217
for (int i = 0; i < nums.length - 3; i++) {
13-
if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates
18+
if (i > 0 && nums[i] == nums[i - 1]) {
19+
continue; // Added curly braces for 'if'
20+
}
1421
for (int j = i + 1; j < nums.length - 2; j++) {
15-
if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates
22+
if (j > i + 1 && nums[j] == nums[j - 1]) {
23+
continue; // Added curly braces for 'if'
24+
}
1625
int left = j + 1, right = nums.length - 1;
1726
while (left < right) {
1827
int sum = nums[i] + nums[j] + nums[left] + nums[right];
1928
if (sum == target) {
2029
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
21-
while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates
22-
while (left < right && nums[right] == nums[right - 1]) right--; // Skip duplicates
30+
while (left < right && nums[left] == nums[left + 1]) {
31+
left++; // Added curly braces for 'while'
32+
}
33+
while (left < right && nums[right] == nums[right - 1]) {
34+
right--; // Added curly braces for 'while'
35+
}
2336
left++;
2437
right--;
2538
} else if (sum < target) {
26-
left++;
39+
left++; // Added curly braces for 'if'
2740
} else {
28-
right--;
41+
right--; // Added curly braces for 'else'
2942
}
3043
}
3144
}
3245
}
3346
return result;
3447
}
3548

49+
// Private constructor to prevent instantiation (utility class)
50+
private FourSum() {}
51+
3652
public static void main(String[] args) {
3753
int[] arr1 = {1, 0, -1, 0, -2, 2};
3854
int target1 = 0;
39-
System.out.println(fourSum(arr1, target1));
55+
System.out.println(findFourSum(arr1, target1)); // Updated method call
4056

4157
int[] arr2 = {4, 3, 3, 4, 4, 2, 1, 2, 1, 1};
4258
int target2 = 9;
43-
System.out.println(fourSum(arr2, target2));
59+
System.out.println(findFourSum(arr2, target2)); // Updated method call
4460
}
4561
}

0 commit comments

Comments
 (0)