LeetCode每日一题 008 两数之和
2019-03-06 01:16
288 查看
1、解题思路
思路一:
双层循环,遍历查找每一个元素 与其匹配的 另一值是否在数组中。优点是直观易表示,确定是时间复杂度为o(n*n);
思路二:
空间换时间,使用哈希表保存所有元素与其位置的关系,则在已知其中一个元素A与target的条件下,另一个元素B=targe-A,再通过hash表查找,可在o(1)时间得到B元素位置。
2、有效题解
[code]/** * Note: The returned array must be malloced, assume caller calls free(). */ typedef struct hashNode { int node_key; int node_value; } hashNode_t; typedef struct hashMap { hashNode_t **node_array; int map_size; } hashMap_t; hashMap_t *hashMap_create(int size) { hashMap_t *map = (hashMap_t *)malloc(sizeof(hashMap_t)); map->node_array = (hashNode_t **)malloc(sizeof(hashNode_t *) * size); int i; for (i = 0; i < size; i++) { map->node_array[i] = NULL; } //memset(map->node_array, 0, size); map->map_size = size; return map; } int hash_fn(int key, int map_size) { return abs(key) % map_size; } hashNode_t *hashMap_get(int key, hashMap_t *map) { int index = hash_fn(key, map->map_size); hashNode_t *node = map->node_array[index]; while (NULL != node && key != node->node_key) { if (index == map->map_size - 1) { index = 0; } else { index++; } node = map->node_array[index]; } return node; } void hashMap_put(int key, int value, hashMap_t *map) { int index = hash_fn(key, map->map_size); hashNode_t *node; while ((node = map->node_array[index])) { if (index == map->map_size - 1) { index = 0; } else { index++; } } node = (hashNode_t *)malloc(sizeof(hashNode_t)); node->node_key = key; node->node_value = value; map->node_array[index] = node; } int* twoSum(int* nums, int numsSize, int target) { if (NULL == nums || numsSize <= 1) { return NULL; } int *ret = (int *)malloc(sizeof(int) * 2); assert(NULL != ret); hashMap_t *map = hashMap_create(numsSize); int i; int other_num; hashNode_t *node; int has_find = 0; for (i = 0; i < numsSize; i++) { other_num = target - nums[i]; if ((node = hashMap_get(other_num, map)) != NULL) { ret[0] = i < node->node_value ? i : node->node_value; ret[1] = i > node->node_value ? i : node->node_value; has_find = 1; break; } else { hashMap_put(nums[i], i, map); } } if (!has_find) { free(ret); ret = NULL; } return ret; }
3、复杂度分析
哈希查找法,遍历数组简历哈希表,时间复杂度o(n),增加额外哈希表,空间复杂度o(n)。
4、小结
1、查找时使用哈希表可在o(1)时间复杂度完成,常用来空间换取时间。
2、哈希表几个关键要素:
1)记录<key, value>结构的hash_node, 保存hash_node的数组table
2)哈希方法hash_fn,常见的直接寻址、平方取中等 (本例注意abs(key))
3)哈希冲突解决办法,常见的冲突链、开放寻址、再散列法。
3、指针数组的用法
4、哈希表的内存释放(本例未完整考虑,有内存泄露)
5、memset是逐字节填充的,
相关文章推荐
- (LeetCode每日一刷01)两数之和
- Leetcode每日一道 -- 两数之和
- LeetCode每日一题 009 两数相加
- Leetcode每日一道 -- 两数相加
- LeetCode 2. Add Two Numbers(两数相加)
- leetcode 每日一题 107. Binary Tree Level Order Traversal II
- 【leetcode】两数之和
- leetcode 两数之和
- leetcode:203 Remove Linked List Elements-每日编程第四十题
- (LeetCode每日一刷05)最长公共前缀
- 167. 两数之和Ⅱ Leetcode
- LeetCode - 两数相加
- leetcode 1. 两数之和
- 每日一练之Palindrome Number【LeetCode No.9】—判断是否为回文数
- 【leetcode每日一题】NO83.Remove Duplicates from Sorted List
- leetcode第一天 两数之和
- 再从萌新开始-Leetcode每日题解-667. Beautiful Arrangement II
- 哈希表学习笔记及LeetCode编程练习之两数之和and快乐数
- 再从萌新开始-Leetcode每日题解-345. Reverse Vowels of a String
- leetcode(1)两数求和 (202)快乐数