力扣hot热题第一题

北慕城南 提交于 2020-02-22 18:00:09

力扣hot热题第一题

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 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){
	int *x=(int*)malloc(sizeof(int)*2); // 分配2个int空间
    int a=0,b=0; 
	for(int i=0;i<numsSize-1;i++){
		a=nums[i];
		for(int j=i+1;j<numsSize;j++){
			b=nums[j];
			if(a+b==target){
				x[0]=i;
                x[1]=j;
                *returnSize=2;
                return x;
			}
		}
	}
  returnSize=0;
	return x;
}

思路分析

因为假设每种输入只会对应一个答案,所以首先把数组中的第一个固定下来设为a,将a的下一个数设置为b,因为a+b=b+a,故b只要从下一个一直到结尾就可以了。

注意事项

注意a只能取到倒数第二个,否则b就越界了。

下标i和j必须搁在一个数组里,数组定义还不能仅仅是int x[],还必须是

int *x=(int*)malloc(sizeof(int)*2); 

大神解法

1、思路一:哈希表

  • 实现Two Sum 如果通过暴力法是很容易解决的,若要考虑效率,可以采用hashmap减少查询的时间。
  • 标签:hash表
  • 采用除留余数法构造hash,将key值作为value的索引,以key-value键值对的方式进行数据存储。
  • 采用链地址法解决冲突,将冲突的hash值链接到链尾。
  • 使用一层for循环遍历数组nuns,i为当前数组的下标,将target与遍历的每一个nums[i]产生差值,在hashmap中查找是否存在该值,若存在,则返回两个元素所在数组的下标。
  • 时间复杂度:O(n)
    空间复杂度:O(n)
typedef struct HashData
{
	int key;
	int value;
}HashData;

typedef struct HashNode
{
	HashData data;
	struct HashNode * next;
}HashNode;

typedef struct HashMap
{
	int size;
	HashNode * table;
}HashMap;

HashMap * CreateHashMap(int *nums,int numSize)
{
	int i;
	int index=0;
	HashMap *hashmap = (HashMap *)malloc(sizeof(HashMap));		
	hashmap->size = numSize;
	hashmap->table = (HashNode *)malloc(sizeof(HashNode)*hashmap->size);	

//初始化hash表
	for(i=0;i<hashmap->size;i++)
	{
		hashmap->table[i].data.value = INT_MIN;
		hashmap->table[i].next = NULL;
	}

//hash表创建
	while(index<numSize)
	{
		int place = abs(nums[index])%(hashmap->size);
		if(hashmap->table[place].data.value == INT_MIN)
		{
			//不冲突
			hashmap->table[place].data.value = nums[index];
			hashmap->table[place].data.key = index;
		}
		else
		{
			//冲突
			//给冲突的元素分配一个节点
			HashNode * node_1 = (HashNode *)malloc(sizeof(HashNode));
			HashNode * hashnode;
			node_1->data.value = nums[index];
			node_1->data.key = index;
			node_1->next = NULL;

			//将节点node_1 与next进行连接
			hashnode = &(hashmap->table[place]);
			while(hashnode->next != NULL)
			{
				hashnode = hashnode->next;
			}
			hashnode->next = node_1;
		}
		index++;
	}
	return hashmap;
}

int GetHashMap(HashMap * hashmap, int value)
{
	int place = abs(value)%(hashmap->size);
	HashNode * pointer = &(hashmap->table[place]);
	while(pointer != NULL)
	{
		if(pointer->data.value == value)
		{
			return pointer->data.key;
		}
		else
		{
			pointer = pointer->next;
		}
	}
	return -1;
}

void freeHashMap(HashMap * hashmap)
{
	int i=0;
	HashNode * Fpointer;
	while(i<hashmap->size)
	{
		Fpointer = hashmap->table[i].next;
		while(Fpointer != NULL)
		{
			hashmap->table[i].next = Fpointer->next;
			free(Fpointer);
			Fpointer = hashmap->table[i].next;
		}
		i++;
	}
	free(hashmap->table);
	free(hashmap);
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
	int i;
	int key=0;
	HashMap * hashmap = CreateHashMap(nums,numsSize);
	int * res = (int *)malloc(sizeof(int)*2);
	* returnSize = 0;
	for(i=0;i<numsSize;i++)
	{
		int value = target - nums[i]; 
		key = GetHashMap(hashmap,value);
		if(key !=-1 && key != i)
		{
			res[0] = i;
			res[1] = key;
            * returnSize=2;
			break;
		}
	}
	freeHashMap(hashmap);
	return res;
}

链接:https://leetcode-cn.com/problems/two-sum/solution/xiao-bai-liang-shu-zhi-he-hashbiao-jie-jue-by-incl/

2、思路二

通过迭代将元素添加到哈希表中,同时我们比较该元素的对应元素是否已经存在与哈希表中,如果存在,我们直接返回答案。

链接:https://leetcode-cn.com/problems/two-sum/solution/tu-jie-ha-xi-biao-by-ml-zimingmeng/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!