mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 1.c
This commit is contained in:
parent
8e77a6ead0
commit
283349b569
46
LeetCode-CN/1.c
Normal file
46
LeetCode-CN/1.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
typedef struct pack
|
||||
{
|
||||
int val;
|
||||
int pos;
|
||||
}pack;
|
||||
|
||||
int qcmp(const void* a, const void* b)
|
||||
{
|
||||
return ((pack*)a)->val - ((pack*)b)->val;
|
||||
}
|
||||
|
||||
int* twoSum(int* nums, int numsSize, int target) {
|
||||
pack* p = (pack*)malloc(sizeof(pack)*numsSize);
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
{
|
||||
p[i].val = nums[i];
|
||||
p[i].pos = i;
|
||||
}
|
||||
qsort(p, numsSize, sizeof(pack), qcmp);
|
||||
|
||||
int L = 0;
|
||||
int R = numsSize - 1;
|
||||
while (L < R)
|
||||
{
|
||||
int sum = p[L].val + p[R].val;
|
||||
if (sum == target)
|
||||
{
|
||||
int* m = (int*)malloc(sizeof(int) * 2);
|
||||
m[0] = p[L].pos;
|
||||
m[1] = p[R].pos;
|
||||
return m;
|
||||
}
|
||||
else if (sum < target)
|
||||
{
|
||||
L++;
|
||||
}
|
||||
else if (sum > target)
|
||||
{
|
||||
R--;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
Loading…
Reference in New Issue
Block a user