LeetCode_AlgorithmProblem01,TwoSum

LeetCode_AlgorithmProblem01,TwoSum

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].

Solution1:枚举法

int* twoSum(int* nums, int numsSize, int target) {
    int i=0,j=0;
    int *twosum = (int *)malloc(sizeof(int) * 2);
    while(i<numsSize){
        j=i+1;
        while(j<numsSize){
            if(nums[i]+nums[j]==target){
                printf("%2d%2d",i,j);
                twosum[0]=i;
                twosum[1]=j;
                return  twosum; 
            }
            j++;
        }
    i++;
    }
    return  twosum;
}

Result1:89ms/64.95%

Solution2:Hash表法,用target减去num1,然后在Hash表中查找是否存在num2。

int* twoSum(int* nums, int numsSize, int target) {
      typedef struct num{
        int value;
        int pos;
      } Nums;
      Nums *nums_hash=(Nums*)malloc(sizeof(Nums)*numsSize);
      int k=0;
      for(k=0;k<numsSize;k++)
            nums_hash[k].value=0;
            nums_hash[k].pos=0;

      int *twosum=(int*)malloc(sizeof(int)*2);
      //为数组建立Hash表 
      int i=0;
      while(i<numsSize){

            int temp=abs(nums[i]);
            if( nums_hash[temp%numsSize].value==0)
             {
                nums_hash[temp%numsSize].value=nums[i];
                nums_hash[temp%numsSize].pos=i;
             }
            else{
                while(1){
                    if(nums_hash[temp%numsSize].value==0){
                        nums_hash[temp%numsSize].value=nums[i];
                        nums_hash[temp%numsSize].pos=i;
                        break; 
                    }
                    temp++;         
                }       
            }
            i++;
        }

      //Hash表中查找 
      int j=0;
      i=0;
      int key; 
      while(i<numsSize){
          int key=target-nums[i];
          if(nums_hash[abs(key)%numsSize].value==key&&i!=nums_hash[abs(key)%numsSize].pos){
            twosum[0]=i,twosum[1]=nums_hash[abs(key)%numsSize].pos;
            // printf("%2d,%d",twosum[0],twosum[1]);
            return twosum;
          } 
          else{
                j=abs(key)%numsSize+1;
                while(j<numsSize){
                    if(nums_hash[j].value==key){
                        twosum[0]=i,twosum[1]=nums_hash[j].pos;
                        // printf("%2d,%d",twosum[0],twosum[1]);
                        return twosum;  
                    }
                    j++;    
                }
            }
          i++;
        } 
    return twosum;
}

Result2:59ms/82.37%

Solution3:HashMap法

typedef struct{
int key;
int val; 
}DataType;
typedef struct{
    DataType data;
    struct HashNode *next; 
}HashNode;
typedef struct{
    int size;
    HashNode *table;
}HashMap,*hashmap;

//f1_createHashMap
HashMap *CreateHashMap(int *nums,int size){
    HashMap *hashmap=(HashMap*)malloc(sizeof(HashMap));
    hashmap->size=2*size;
    hashmap->table=(HashNode *)malloc(sizeof(HashNode)*hashmap->size);
    int j=0;
    for(j=0;j<hashmap->size;j++){
        hashmap->table[j].data.val=INT_MIN;
        hashmap->table[j].next=NULL;
    }
    int i=0;
    while(i<size){
        int pos=abs(nums[i])%hashmap->size;
        if(hashmap->table[pos].data.val==INT_MIN){
            hashmap->table[pos].data.key=i;
            hashmap->table[pos].data.val=nums[i];
        }
        else{
            HashNode *lnode=(HashNode*)malloc(sizeof(HashNode)),*hashnode;
            lnode->data.key=i;
            lnode->data.val=nums[i];
            lnode->next=NULL;
            hashnode=&(hashmap->table[pos]);
            while(hashnode->next!=NULL){
                hashnode=hashnode->next;
            }
            hashnode->next=lnode;
        }
        i++;    
    }
    return hashmap;
}

//f2_PrintHashMap 
void PrintHashMap(HashMap* hashmap){
    int i=0;
    HashNode *pointer;
    while(i<hashmap->size){
        pointer=&(hashmap->table[i]);
        while(pointer!=NULL){
            printf("%10d",pointer->data.val);
            pointer=pointer->next;
        }
        i++;
        printf("\n");
    }
}
//f3_GetHashNode
int* Get(int value,HashMap hashmap){
    int pos=abs(value)%hashmap.size;
    HashNode *pointer=&(hashmap.table[pos]);
int i=0;
    while(pointer!=NULL){
            if(pointer->data.val==value) 
    return pointer->data.key;
    else
        pointer=pointer->next;

        }

    return -1;
}   

int* twoSum(int* nums, int numsSize, int target) {
   int *twosum=(int *)malloc(sizeof(int)*2);
   HashMap *hashmap=CreateHashMap(nums,numsSize);
   int i=0;
   while(i<numsSize){
    int goal=target-nums[i];
    if(Get(goal,*hashmap)!=-1&&Get(goal,*hashmap)!=i){
        twosum[0]=i,twosum[1]=Get(goal,*hashmap);
        break;
       }
    i++;  
   } 
   return  twosum;
}

Result3:3ms/89.97%


HashMap数据结构如下图:
HashMap示意图

三种算法分析与比较:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值