题目描述:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的两个整数。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
题目解答:
方法1:暴力解法
两层for循环,思路简单,但时间复杂度为O(n^2),较慢,运行时间100ms左右,代码如下。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int i = 0, j = 0;
int n = numsSize;
int* result = NULL;
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(target == nums[i] + nums[j]) {
result = (int*)malloc(sizeof(int) * 2);
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
方法2:HASH
对于查找目标元素是否存在,hash是一种非常快的方式。但C语言没有标准的hash函数,所以需要自己实现一个简单的hash函数,包括插入和查询。这里采用拉链法hash,即冲突的元素使用链表连在一起。因为直接用数字对长度取余做hash索引,所以需要注意的数字的正负及数字的范围(是否会超过int的范围)。对于某一个元素nums[i]
,如果hash表中没出现过target - nums[i]
,则需要把nums[i]
放入hash表,否则直接返回结果。运行时间4ms,代码如下。
或者先求出数组中最小值min和最大值max,申请一个max-min
长的数组hashTable,用该数组做hash,hashTable[nums[i] - min] = i
,不过如果max-min
特别大的话,就会需要很大的空间。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct node {
long val;
int index;
struct node* next;
};
void insert(struct node** hashTable, long val, int index, int n) {
int t = abs(val) % n;
struct node* temp = hashTable[t];
// head-add
struct node* add = (struct node*)malloc(sizeof(struct node));
add->val = val;
add->index = index;
add->next = temp->next;
temp->next = add;
}
int search(struct node** hashTable, long target, int n) {
int index = abs(target) % n;
struct node* temp = hashTable[index]->next;
while(temp) {
if(temp->val == target) {
return temp->index;
}
temp = temp->next;
}
return -1;
}
void freeHashTable(struct node** hashTable, int n) {
int i = 0;
struct node *temp = NULL, *delete = NULL;
for(i = 0; i < n; i++) {
temp = hashTable[i];
delete = temp;
while(temp) {
delete = temp;
temp = temp->next;
free(delete);
}
}
free(hashTable);
}
int* twoSum(int* nums, int numsSize, int target) {
int i = 0, j = 0;
int n = numsSize, index = 0;
int* result = NULL;
struct node** hashTable = (struct node**)malloc(n * sizeof(struct node*));
// init head node
for(i = 0; i < n; i++)
hashTable[i] = (struct node*)calloc(1, sizeof(struct node));
for(i = 0; i < n; i++) {
index = search(hashTable, target - nums[i], n);
if(-1 == index)
insert(hashTable, nums[i], i, n);
else {
result = (int*)malloc(sizeof(int) * 2);
result[0] = index;
result[1] = i;
freeHashTable(hashTable, n);
return result;
}
}
freeHashTable(hashTable, n);
return result;
}
方法3:排序+双下标
构造结构体,将数字与其原始位置关联起来。然后对结构体数组进行排序,从小到大的顺序,这里采用qsort()
函数。利用两个下标begin和end,begin指向于开始的位置,end指向结束的位置,判断nums[begin] + nums[end]
与target的关系,如果相等,则直接返回索引;如果比target大,则说明需要减小两数之和,即end--
,来减小后者数字;如果比target大,则说明需要增大两数之和,即begin++
来增大前者数字。通过这种方式逐渐逼近target,运行时间0ms,代码如下。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct node {
int val;
int index;
};
int comp(const void* a, const void* b) {
return (((struct node*)a)->val > ((struct node*)b)->val ? 1 : -1);
}
int* twoSum(int* nums, int numsSize, int target) {
int i = 0;
int n = numsSize;
int begin = 0, end = n - 1;
int* result = NULL;
struct node* nodes = (struct node*)malloc(n * sizeof(struct node));
// init nodes
for(i = 0; i < n; i++) {
nodes[i].val = nums[i];
nodes[i].index = i;
}
qsort(nodes, n, sizeof(struct node), comp);
while(begin < end) {
if(nodes[begin].val + nodes[end].val == target) {
result = (int*)malloc(sizeof(int) * 2);
result[0] = nodes[begin].index;
result[1] = nodes[end].index;
free(nodes);
return result;
}
else if(nodes[begin].val + nodes[end].val > target)
end--;
else
begin++;
}
free(nodes);
return result;
}