-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.cpp
More file actions
48 lines (44 loc) · 1.18 KB
/
TwoSum.cpp
File metadata and controls
48 lines (44 loc) · 1.18 KB
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<iostream>
#include<memory.h>
int *twoSum(int numbers[], int n, int target) {
static int result[2];
memset(result, 0, sizeof(result));
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if((numbers[i]+numbers[j]) == target)
{
result[0] = i+1;
result[1] = j+1;
return result;
}
}
}
return result;
}
int *twoSum(int numbers[], int n, int target) {
int i,j;
static int result[2] = {};
int bucket[100000] = {};
for(i = 0;i < n;i++){
bucket[numbers[i]] += 1;
}
for(i = 0;i < n;i++){
if(bucket[target - numbers[i]])
for(j = n - 1;j > i;j--){
if(numbers[i] + numbers[j] == target){
result[0] = i + 1,result[1] = j + 1;
}
}
}
return result;
}
int main()
{
int numbers[] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82};
int* res = twoSum(numbers, 42, 8);
//std::cout<<*res<<" "<<*(++res)<<std::endl;
std::cout<<*res;
std::cout<<*(++res)<<std::endl;
}