[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(
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(
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;}
阅读全文
0 0
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- LeetCode | Two Sum
- 2017百毒之星第一场
- IBM Minus One 简单字符处理
- Spring MVC:从零开始
- Javascript中的this,call,apply,bind函数
- JavaScript执行环境可以访问变量的类型及优先顺序
- [leetCode]Two Sum
- java中,a=a+b与a+=b的异同
- RedHat7 安装redis
- 8月12日总结
- 使用Struts2开发程序的基本步骤
- JavaScript的闭包
- ACM集训日记-8月12日
- 真正从零开始,TensorFlow详细安装入门图文教程!
- 让你的情商爆棚(提高情商的方法)