-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_sum.c
48 lines (45 loc) · 898 Bytes
/
two_sum.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int find_if_two_sum(int *arr, int i, int j, int target) {
if (!arr) {
return 0;
}
while (i < j) {
if (arr[i] + arr[j] < target) {
i++;
} else if (arr[i] + arr[j] > target) {
j--;
} else {
return 1;
}
}
return 0;
}
void find_two_sums(int *arr, int i, int j, int target) {
if (!arr) {
return;
}
while (i < j) {
if (arr[i] + arr[j] == target) {
printf("%d, %d\n", arr[i], arr[j]);
// 处理重复的逻辑
while (i < j && arr[i] == arr[i + 1]) {
i++;
}
while (i < j && arr[j] == arr[j - 1]) {
j--;
}
// 如果是找到了,继续找
i++;
} else if (arr[i] + arr[j] < target) {
i++;
} else if (arr[i] + arr[j] > target) {
j--;
}
}
}
int main(int argc, char *argv[]) {
int arr[] = {1,2,3,3,4,5,5,6,6,6};
find_two_sums(arr, 0, sizeof(arr)/sizeof(int) - 1, 10);
}