-
Notifications
You must be signed in to change notification settings - Fork 27
/
16. 3Sum Closest.c
56 lines (45 loc) · 1.42 KB
/
16. 3Sum Closest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int threeSumClosest(int* nums, int numsSize, int target) {
int i, x, y, k, d, d1;
qsort(nums, numsSize, sizeof(int), cmp);
d = 0;
for (i = 0; i < numsSize - 2; i ++) {
x = i + 1;
y = numsSize - 1;
while (x < y) {
k = nums[i] + nums[x] + nums[y];
if (k == target) {
return k;
} else if (k > target) {
y --;
} else {
x ++;
}
d1 = k - target;
if (d == 0 || abs(d) > abs(d1)) {
d = d1;
}
}
}
return target + d;
}
/*
Difficulty:Medium
Total Accepted:136.6K
Total Submissions:439.3K
Companies Bloomberg
Related Topics Array Two Pointers
Similar Questions
3Sum
3Sum Smaller
*/