1,Two sum (Hashtable Array)

/*Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target,
where index1 must be less than index2.Please note that your returned answers(both index1 and index2)
are not zero - based.

You may assume that each input would have exactly one solution.

Input: numbers = { 2, 7, 11, 15 }, target = 9
Output : index1 = 1, index2 = 2

*/

#include<stdlib.h>
#include<stdio.h>

方法一: brute-force
//时间复杂度是O(n^2)太大 ,大数据量会出现 Time Limit Exceeded
int* twoSum1(int * nums, int numsSize , int target) {
           int i, j;
           for (i = 0; i<numsSize ; i++){
                    for (j = i + 1; j<numsSize ; j++){
                              if ((nums [i] + nums[j]) == target){
                                       int* index = (int *)malloc(sizeof( int)* 2);
                                       if (i<j){

                                                index[0] = i + 1;
                                                index[1] = j + 1;
                                      }
                                       else{
                                                index[0] = j + 1;
                                                index[1] = i + 1;
                                      }
                                       return index;

                             }
                   }
          }
           return NULL ;
}


方法二,下面使用java的HashMap数据结构,时间复杂度O(n),空间复杂度是O(n)

public int [] twoSum(int[] nums, int target) {
          if(nums == null || nums.length <= 0){
              return null ;
         }
         
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        int len = nums.length ;
       
        for(int i=0; i<len; i++){
          int firstNum = nums[i];
          int secondNum = target-firstNum;
         
         Integer result = map.get(secondNum);
         
          if(result != null){
              int arr[] = new int[2];
              if(result > i){
                 arr[0] = i+1;
                 arr[1] = result+1;
             } else{
                 arr[0] = result+1;
                 arr[1] = i+1;
             }
             
              return arr;
         }
         
         map.put(firstNum, i);
         
        }
          return null ;
    }

方法三:使用C语言对HashMap进行模仿
typedef struct MapNode{
           int key;
           int value;
           struct MapNode * next;
}MapNode;

void put(MapNode *map, int key , int value, int len ){
           int index = key %   len;

           MapNode* node = (MapNode *)malloc(sizeof( MapNode));
          node->key = key;
          node->value = value;

           if (map [index].next == NULL){
                   node->next = NULL;
          }
           else{
                   node->next = map[index].next;
          }

           map[index].next = node;
}

int get(MapNode *map, int key , int len){
           int index = key %   len;
           MapNode* node = map [index].next;

           while (node != NULL ){
                    if (node->key == key ){
                              return node->value;
                   }

                   node = node->next;
          }

           return           -999;
}

int* twoSum(int * nums, int numsSize , int target){
           if (nums == NULL || numsSize <= 0){
                    return NULL ;
          }

           MapNode *map = (MapNode *)malloc(sizeof( MapNode)*numsSize );
           for (int i = 0; i < numsSize; i++){
                   map[i].next = NULL;
          }

           for (int i = 0; i < numsSize; i++){
                    int firstNumber = nums [i];
                    int secondNumber = target - firstNumber;

                    int result = get(map, secondNumber, numsSize);

                    if (result != -999){
                              int* arr = (int *)malloc(sizeof( int)* 2);

                              if (i >result){
                                      arr[0] = result + 1;
                                      arr[1] = i + 1;
                             }
                              else{
                                      arr[0] = i + 1;
                                      arr[1] = result + 1;
                             }

                              return arr;
                   }

                   put(map, firstNumber, i, numsSize);
          }


           return NULL ;
}

int main(){

           int array[4] = {0,4,3,0};
           int *result = twoSum(array, 4, 0);
          printf( "[%d ,%d]\n", *result, *(result + 1));

          printf( "Hello world \n");
           return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值