leetcode_twoSum

leetcode_twoSum


int* twoSum(int* nums,int numSize,int target)
{
int *a = (int *)malloc(2*sizeof(int));
for (int i = 0;i < numSize;i ++)
{
for (int j = i + 1;j<numSize && j != i ;j++)
{
if(nums[i] + nums[j] == target)
{
a[0] = i;
a[1] = j;
}
}

}
return a;
}


int* twoSum1(int* nums,int numSize,int target)
{
int min = 2147483647;
int i = 0;
for (i = 0;i < numSize;i ++)
{
if (nums[i] < min)
{
min = nums[i];
}
}

int max = target - min;
int len = max - min + 1;
int *table = (int *)malloc(len*sizeof(int));
int *indice = (int *)malloc(2*sizeof(int));
for (int i = 0;i < len;i++)
{
table[i] = -1;
}

for (int i = 0;i < numSize; i ++)
{
if (nums[i]-min < len)
{
if (table[target-nums[i]-min] != -1)
{
indice[0] = table[target - nums[i] - min];
indice[1] = i;
return indice;
}
table[nums[i] - min] = i;
}
}
free(table);
return indice;
}



Loading Disqus comments...
Table of Contents