Initial commit
This commit is contained in:
28
1-1-two-sum.cpp
Normal file
28
1-1-two-sum.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
|
||||
int *ans = (int *)malloc(2 * sizeof(int));
|
||||
*returnSize = 2;
|
||||
for (int i = 0; i < numsSize - 1; i++) {
|
||||
for (int j = i + 1; j < numsSize; j++) {
|
||||
if (nums[i] + nums[j] == target) {
|
||||
ans[0] = i;
|
||||
ans[1] = j;
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int nums[] = {3,3};
|
||||
int target = 6;
|
||||
int *ans = NULL;
|
||||
int ans_size = 0;
|
||||
ans = twoSum(nums, sizeof(nums) / sizeof(int), target, &ans_size);
|
||||
printf("size = %d, ans = [%d, %d]\n", ans_size, ans[0], ans[1]);
|
||||
free(ans);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user