您的位置:首页 > 其它

[Leetcode题解]001 Two Sum

2015-07-02 09:33 513 查看

[Leetcode题解] 1 Tow Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

题目传送门:Two sum

思路

  这道题目最容易想的方法是暴力法,遍历每一种可能的组合,并判断结果是否等于target。这种方法的时间复杂度是O(n^2),结果超时。

  后来想到了一种HASH表的方法,只需遍历一遍数组,如果
nums[i]
没有在hash表中,则将
target-nums[i]
作为键,
i
作为值存储在hash表中。否则找到,返回hash表中键为
nums[i]
的键值对的值,并计算两个索引返回。这种方法时间复杂度是O(n)。

  还有一种方法,先把数组从小到大排序,再用两个指针从数组两头向中间夹。初始时
left = 0
rigth = numsSize-1
,然后如果
nums[left] + nums[right] < target
,则
left++
;如果
nums[left] + nums[right] > target
,则
right--
;否则找到结果并返回。在这一题中这种思路有个问题是要主要保存排序前数字对应的索引。这种方法时间复杂度是O(nlogn)。

解法

  我采用的是第二种方法(Hash表),代码如下:

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int * twoSum(int* nums, int numsSize, int target) {
int * indexs;
int i;

indexs = (int *)malloc(2 * sizeof(int));
memset(indexs, 0, sizeof(indexs));

node * ht
;
for(i = 0; i < N; i++)
{
ht[i] = NULL;
}

for(i = 0; i < numsSize; i++)
{
node * p = search_by_key(ht, nums[i]);
if(p)
{
/*如果存在,那么找到,索引为p->data.value+1和i+1 */
indexs[0] = p->data.value + 1;
indexs[1] = i + 1;
return indexs;
}
else
{
/* 如果不存在,就将target-nums[i],i存储在哈希表中*/
insert(ht, target-nums[i], i);
}
}
return indexs;
}


  由于这道题是我在leetcode上做到第一道题,当时不知道有uthash.h这个东西,所以就自己实现了一个哈希表,如下所示:

#define N 100
#define abs(x) (((x)>0)?(x):(-x))

typedef struct DATATYPE{
int key;
int value;
}data_type;

typedef struct NODE{
data_type   data;
struct NODE * next;
}node;

/**
* 搜索hash表
*/
node * search_by_key(node * ht
, int key)
{
node * p;
int addr = abs(key % N);
p = ht[addr];

while(p && p->data.key != key)
p = p->next;

return p;
}

/**
* 插入hash表
*/
void insert(node * ht
, int key, int value)
{

int addr;
node * p, * s;

s = (node *)malloc(sizeof(node));
s->data.key = key;
s->data.value = value;

p = search_by_key(ht, key);

if(p)
{/*记录存在*/
free(s);
}
else
{
addr = abs(s->data.key % N);
s->next = ht[addr];
ht[addr] = s;
}
}


  以后用到hash表可以使用uthash.h了,使用方法请参考http://www.cnblogs.com/pure/archive/2012/08/29/2661996.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash c leetcode