1.Two Sum(两数之和)

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

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


方法一:暴力法
暴力法实际上很简单,只要遍历每个元素x,并查找是否存在一个值和
target-x相等就可以了。但是缺点也是很明显的,它的时间复杂度达到了O(n2),这显然不
是我们要的结果。(注:该方法很简单,就不写代码)。
方法二:Hash法
 实际上,当我们遍历数组去查找和target-x相等的数时候,每一次都要从头开始遍历。而其实
在x前面的数其实已经被访问过多次了,这种行为确实是比较浪费时间的。而这时候我们需要一个
表来记录已经访问过的数据,并且访问这个表中的数据的时间复杂度越低越好,而Hash表显然符合
这个性质,并且其访问数据在理论在可以达到O(1)。
本来想偷个懒用一个简单的数组来模拟Hash表的功能,结果leetcode给的测试用例中数覆盖很全
(nums里面有很大的数),发现通不过.看来还是要老老实实写Hash表。
偷懒版:

int* twoSum(int* nums, int numsSize, int target) {
  int m[1000001] = {0},*n = m + 500000;
  int i = 0;
  for (;i < numsSize; i++) {
    int poor = target-nums[i];
    if (n[poor] > 0) {
      int *value_index = malloc(sizeof(int) * 2); 
      value_index[0] = i;
      value_index[1] = n[poor] - 1;
      return value_index;
    }   
    else
      n[nums[i]] = i + 1;
  }
  return NULL;
}
 
 
老实版:
typedef struct node{
  int key;
  int value;
  struct node *next;
}node_t;

typedef struct hash_table {
  int capacity;
  node_t **hashtab;
}hash_table_t;

hash_table_t *hash_table_create(int capacity)
{
  hash_table_t *hash_table = (hash_table_t *)malloc(sizeof(hash_table_t));
  hash_table->hashtab = (node_t **)malloc(sizeof(node_t*) * capacity);
   for(int i = 0;i < capacity;i++)
       hash_table->hashtab[i] = NULL;
   
  hash_table->capacity = capacity;
    return  hash_table;
}
int get_hash_key(int n, int capacity){
  return abs((65535 * n + 1048575)) % capacity;
}

int hash_table_value(hash_table_t *hash_table,int key)
{
  int index = get_hash_key(key,hash_table->capacity);
  node_t *curr = hash_table->hashtab[index];
  while(curr != NULL){
    if(curr->key == key)
      return curr->value;
    curr = curr->next;
  }

  return -1; 
}
void hash_table_add(hash_table_t *hash_table,int key,int value)
{
  int index = get_hash_key(key,hash_table->capacity);//get hash key

  node_t *node = (node_t*)malloc(sizeof(node_t));
  node->next = NULL;
  node->next   = hash_table->hashtab[index];
  node->key    = key;
  node->value  = value;
  hash_table->hashtab[index] = node;
}

void hash_table_free(hash_table_t* hash_table)
{
  for(int i=0;i<hash_table->capacity;i++){
    node_t* curr = hash_table->hashtab[i];
    while(curr!=NULL){
      node_t* hold = curr;
      curr->next = curr;
      free(hold);

    }   

  }
  free(hash_table->hashtab);
  free(hash_table);

}

int* twoSum(int* nums, int numsSize, int target)
{
  hash_table_t* hash_table = hash_table_create(numsSize/2);

  for(int index=0;index<numsSize;index++){
    int idx = hash_table_value(hash_table, target - nums[index]);

    if(idx>=0){
      int* result = (int*)malloc(sizeof(int)*2);
      result[1] = index;
      result[0] = idx;
      return result;

    }

    hash_table_add(hash_table, nums[index], index);

  }

  return NULL;
}

 

 

转载于:https://www.cnblogs.com/bspp1314/p/9401993.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值