File tree Expand file tree Collapse file tree 1 file changed +3
-11
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +3
-11
lines changed Original file line number Diff line number Diff line change @@ -7,21 +7,13 @@ public class _16 {
7
7
public static class Solution1 {
8
8
public int threeSumClosest (int [] nums , int target ) {
9
9
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
- }
18
10
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 ++) {
20
12
int left = i + 1 ;
21
- int right = len - 1 ;
13
+ int right = nums . length - 1 ;
22
14
while (left < right ) {
23
15
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 )) {
25
17
sum = thisSum ;
26
18
if (sum == target ) {
27
19
return sum ;
You can’t perform that action at this time.
0 commit comments