题目描述
给定一个整型数组,要求返回两个数的下标,使得两数之和等于给定的目标值,要求同一个下标不能使用两次。
数据保证有且仅有一组解。
样例
给定数组 nums = [2, 7, 11, 15],以及目标值 target = 9,
由于 nums[0] + nums[1] = 2 + 7 = 9,
所以 return [0, 1].
算法1:
(暴力枚举)O(n^2)
使用两个循环实现枚举,每一次循环为O(n),两次循环时间复杂度为O(n^2)
C:(执行用时:272ms,内存消耗:7.6M)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *res = (int *)malloc(sizeof(int) * 2);
*returnSize = 0;
for (int i = 0; i < numsSize; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
res[0] = i;
res[1] = j;
*returnSize = 2;
break;
}
}
}
return res;
}
C++:(执行时间:220 ms,内存消耗:9.1M)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[i] + nums[j] == target) {
res = vector<int>({i, j});
break;
}
}
}
return res;
}
};
算法二:使用hash table
通过一次遍历,使用除留余数法和链地址法构造hash table,并且查找
C:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct hashData {
int value;
int key;
struct hashData *next;
};
typedef struct hashTable {
struct hashData **elem_head; // array stored the head of the single list
int width;
}hashTable;
int hashInit(hashTable *table, int width) {
if (width < 0)
return 1;
struct hash_data **tmp = malloc(sizeof(struct hash_data *)*width);
table->elem_head = tmp;
memset(table->elem_head, 0, width * sizeof(struct hash_data *));
table->width = width;
return 0;
}
void hashFree(hashTable *table) {
if (table->elem_head != NULL) {
for (int i = 0; i < table->width; i++) {
struct hashData *elem = table->elem_head[i];
while(elem != NULL) {
struct hashData *temp = elem;
elem = elem->next;
free(temp);
}
}
free(table->elem_head);
table->elem_head = NULL;
}
table->width = 0;
}
int hashFunc(hashTable *table, int value) {
int addr = abs(value) % table->width;
return addr;
}
int hashInsert(hashTable *table, int value, int key) {
int addr = hashFunc(table, value);
struct hashData *tmp = (struct hashData *)malloc(sizeof(struct hashData));
if (tmp == NULL) {
return -1;
}
tmp->value = value;
tmp->key = key;
tmp->next = table->elem_head[addr];
table->elem_head[addr] = tmp;
printf("Insert successfully, value = %d, key = %d\n", tmp->value, tmp->key);
return 0;
}
struct hashData* hashFind(hashTable *table, int value) {
int addr = hashFunc(table, value);
printf("start find value = %d\n", value);
struct hashData *tmp = table->elem_head[addr];
while (tmp != NULL) {
if (tmp->value == value) {
printf("find value = %d, key = %d\n", tmp->value, tmp->key);
return tmp;
}
tmp = tmp->next;
}
printf("cannot find value = %d\n", value);
return NULL;
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *res = (int *)malloc(sizeof(int) * 2);
*returnSize = 0;
hashTable table;
hashInit(&table, 100);
for (int i = 0; i < numsSize; i++) {
int value = target - nums[i];
struct hashData *data = hashFind(&table, value);
if (data != NULL) {
res[0] = data->key;
res[1] = i;
*returnSize = 2;
printf("res: [%d, %d]\n", data->key, i);
break;
}
hashInsert(&table, nums[i], i);
}
hashFree(&table);
return res;
C++:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int value = target - nums[i];
if (hash.count(value)) {
res = vector<int>({hash[value], i});
break;
}
hash[nums[i]] = i;
}
return res;
}
};

827

被折叠的 条评论
为什么被折叠?



