题目
LeetCode: two-sum
暴力破解
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int max = INT_MIN;
int min = INT_MAX;
int * indices = (int *) malloc(sizeof(int)*2);
for (int i = 0; i < numsSize; ++i)
{
if (nums[i] > max) max = nums[i];
if (nums[i] < min) min = nums[i];
}
int * map = (int *) calloc(max-min+1, sizeof(int));
for (int i = 0; i < numsSize; ++i)
{
map[nums[i] - min] = i;
}
for (int i = 0; i < numsSize; ++i)
{
int lookfornum = target - nums[i];
if (lookfornum < min || lookfornum > max) continue;
if (map[lookfornum - min] != 0 && map[lookfornum - min] != i)
{
indices[0] = i;
indices[1] = map[lookfornum - min];
break;
}
}
return indices;
}