Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0cd0897

Browse files
committedOct 21, 2021
refactor 16
1 parent 365ec1c commit 0cd0897

File tree

1 file changed

+3
-11
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+3
-11
lines changed
 

‎src/main/java/com/fishercoder/solutions/_16.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,13 @@ public class _16 {
77
public static class Solution1 {
88
public int threeSumClosest(int[] nums, int target) {
99
Arrays.sort(nums);
10-
int len = nums.length;
11-
if (len < 3) {
12-
int sum = 0;
13-
for (int i : nums) {
14-
sum += i;
15-
}
16-
return sum;
17-
}
1810
int sum = nums[0] + nums[1] + nums[2];
19-
for (int i = 0; i < len - 2; i++) {
11+
for (int i = 0; i < nums.length - 2; i++) {
2012
int left = i + 1;
21-
int right = len - 1;
13+
int right = nums.length - 1;
2214
while (left < right) {
2315
int thisSum = nums[i] + nums[left] + nums[right];
24-
if (Math.abs(target - thisSum) < Math.abs(target - sum)) {
16+
if (Math.abs(thisSum - target) < Math.abs(sum - target)) {
2517
sum = thisSum;
2618
if (sum == target) {
2719
return sum;

0 commit comments

Comments
 (0)
Failed to load comments.