leetcode 1 :两数之和

1. 题目

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

2. 示例

给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

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

}

3. 自己的解答-暴力解法

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
	int* res = (int *)malloc(2*sizeof(int));
	if (res == NULL)
	{
		*(returnSize) = 0;
		return res;
	}
	memset(res, 0, sizeof(res));
	
	for(int i = 0; i < numsSize -1 ; i++)
	{
		for(int j = i+1; j < numsSize; j++)
		{
			if((target - nums[i]) == nums[j])
			//if((target == nums[i]) + nums[j])
			{
				res[0] = i;
				res[1] = j;
				*returnSize = 2;
				return res;
			}
		}
	}
	
	return res;
}

暴力法时间复杂度O(n^2)

4. 标准答案解答

4.1 标准答案解答 - hash 散列

没必要使用C语言实现hash操作,直接使用数组进行散列, 即将 nums 中的元素值当下标,nums的下标当值存储在 hash 数组中 : hash[ nums[i] ] = i;
首先初始化 hash[2000], 初始值设为 -1 ,
遍历数组, 查看 target - nums[i] 为下标 的 hash 数组元素值 (hash[ target - nums[i] ]) 是否为 - 1;
若为 -1 ,将 下标 i 存放在 hash数组的 nums[i] 位置上, hash[ nums[i] ] = i;
若不为 -1 ,即存在 相加为 target 的元素,两个元素的下标为 hash[ target - nums[i]] , i;

作者:dingjinyang
链接:https://leetcode-cn.com/problems/two-sum/solution/cyu-yan-ji-yu-shu-zu-de-san-lie-15xing-dai-ma-8ms-/

//hash 散列
#define MAX_SIZE 2048
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
	int i, hash[MAX_SIZE], *res = (int *)malloc(sizeof(int) * 2);
	memset(hash, -1, sizeof(hash));
	for (i = 0; i < numsSize; i++)
	{
		if (hash[(target - nums[i] + MAX_SIZE) % MAX_SIZE] != -1)
		{
			res[0] = hash[(target - nums[i] + MAX_SIZE) % MAX_SIZE];
			res[1] = i;
			*returnSize = 2;
			return res;
		}
		hash[(nums[i] + MAX_SIZE) % MAX_SIZE] = i;  //防止负数下标越界,循环散列
	}
	free(hash);
	*returnSize = 0;
	return res;
}

4.2 标准答案解答 - hash 表

对于查找目标元素是否存在,hash是一种非常快的方式。
但C语言没有标准的hash函数,所以需要自己实现一个简单的hash函数,包括插入和查询。
这里采用拉链法hash,即冲突的元素使用链表连在一起。
因为直接用数字对长度取余做hash索引,所以需要注意的数字的正负及数字的范围(是否会超过int的范围)。
对于某一个元素nums[i],如果hash表中没出现过target - nums[i],则需要把nums[i]放入hash表,否则直接返回结果。

原文链接:https://blog.csdn.net/hang404/article/details/84764531

/**
* 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;
}

猜你喜欢

转载自blog.csdn.net/lqy971966/article/details/107164539