[leetCode]Two Sum

来源:互联网 发布:网络高弹丝 编辑:程序博客网 时间:2024/05/13 19:27

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

javascript:

/** * @param {number[]} nums * @param {number} target * @return {number[]} */var twoSum = function(nums, target) {    var arr=new Array();    for(var i=0;i<nums.length;i++){        for(var j=i+1;j<nums.length;j++){            if(nums[i]+nums[j]===target){                arr.push(i);                arr.push(j);                return arr;            }        }    }};

Time complexity:O(n2)

c:

#define height 100typedef struct HashNode{    int index;//记录在nums中的下标     int value;//记录nums[i]的值     struct HashNode *next; //下一个节点的位置 }HashNode;typedef struct heads{//  int index;    HashNode *Head; }HashHead;typedef struct HashMap{    HashHead * Hs;}HashMap;HashMap * Init_HashMap(HashMap * map);HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize);void Destroy_HashMap(HashMap **map);int* twoSum(int* nums, int numsSize, int target);HashMap * Init_HashMap(HashMap * map){    //初始化链地址的头部     int i;    map=(HashMap *)malloc(sizeof(HashMap));    map->Hs=(HashHead *)malloc(sizeof(HashHead)*height);    if(map->Hs){        for(i=0;i<height;i++){            map->Hs[i].Head=NULL;        }    }    return map;}HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize){//数组中的数据存入哈希表。     int mod,i;    //将各个num[i]存入哈希表中     for(i=0;i<numsSize;i++){        mod=abs(nums[i])%height;        HashNode *node=(HashNode *)malloc(sizeof(HashNode));        node->index=i;        node->value=nums[i];        node->next=map->Hs[mod].Head;//尾插法         map->Hs[mod].Head=node;    }    return map; }void Destroy_HashMap(HashMap **map){    HashNode *p;    int i;    for(i=0;i<height;i++){        if((*map)->Hs[i].Head){            p=(*map)->Hs[i].Head;            (*map)->Hs[i].Head=(*map)->Hs[i].Head->next;            free(p);        }    }    free((*map)->Hs); }int* twoSum(int* nums, int numsSize, int target) {      int aMod,j;    HashNode *p;    int *result=(int *)malloc(sizeof(int)*2);    HashMap *map;    map=Init_HashMap(map);    map=Create_HashMap(map,nums,numsSize);    for(j=0;j<numsSize;j++){        aMod=abs(target-nums[j])%height;        p=map->Hs[aMod].Head;        while(p!=NULL){            if(p->value==target-nums[j] && p->index!=j){                result[0]=j;                result[1]=p->index;                Destroy_HashMap(&map);//释放内存                return result;//注意这里            }else{                p=p->next;            }         }       }    return result;//虽然上面有返回值,但是这里一定也要有返回值,否则找错会让你非常痛苦}

哈希表在平均的情况下比 O(n2)好很多。但是时间复杂在最坏情况下还是O(n2)。不考虑内存我们将哈希表开大点,没有冲突时,就可以做到O(n)。

c完整源码:

/*    address=num[i]%height;     采用链地址法处理哈希冲突 */#include<stdio.h>#include<stdlib.h>#include<math.h> #define height 7typedef struct HashNode{    int index;//记录在nums中的下标     int value;//记录nums[i]的值     struct HashNode *next; //下一个节点的位置 }HashNode;typedef struct heads{//  int index;    HashNode *Head; }HashHead;typedef struct HashMap{    HashHead * Hs;}HashMap;HashMap * Init_HashMap(HashMap * map);HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize);void Destroy_HashMap(HashMap **map);int* twoSum(int* nums, int numsSize, int target);HashMap * Init_HashMap(HashMap * map){    //初始化链地址的头部     int i;    map=(HashMap *)malloc(sizeof(HashMap));    map->Hs=(HashHead *)malloc(sizeof(HashHead)*height);    if(map->Hs){        for(i=0;i<height;i++){            map->Hs[i].Head=NULL;        }    }    return map;}HashMap * Create_HashMap(HashMap * map,int *nums,int numsSize){//数组中的数据存入哈希表。     int mod,i;    //将各个num[i]存入哈希表中     for(i=0;i<numsSize;i++){        mod=abs(nums[i])%height;        HashNode *node=(HashNode *)malloc(sizeof(HashNode));        node->index=i;        node->value=nums[i];        node->next=map->Hs[mod].Head;//尾插法         map->Hs[mod].Head=node;    }    return map; }void Destroy_HashMap(HashMap **map){    HashNode *p;    int i;    for(i=0;i<height;i++){        if((*map)->Hs[i].Head){            p=(*map)->Hs[i].Head;            (*map)->Hs[i].Head=(*map)->Hs[i].Head->next;            free(p);        }    }    free((*map)->Hs); }int* twoSum(int* nums, int numsSize, int target) {      int aMod,j;    HashNode *p;    int *result=(int *)malloc(sizeof(int)*2);    HashMap *map;    map=Init_HashMap(map);    map=Create_HashMap(map,nums,numsSize);    for(j=0;j<numsSize;j++){        aMod=abs(target-nums[j])%height;        p=map->Hs[aMod].Head;        while(p!=NULL){            if(p->value==target-nums[j] && p->index!=j){                result[0]=j;                result[1]=p->index;                Destroy_HashMap(&map);                return result;            }else{                p=p->next;            }         }       }    return result;//虽然上面有返回值,但是这里一定也要有返回值,否则找错会让你非常痛苦}int main(void){    int nums[7]={-1,-2,-3,-4,-5};    int numsSize,target;    int *arr;    scanf("%d %d",&numsSize,&target);    arr=twoSum(nums,numsSize,target);     printf("%d %d",arr[0],arr[1]);      return 0;} 
原创粉丝点击