【Leet Code】1. Two Sum

  • 2019 年 11 月 8 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/shiliang97/article/details/101386874

1. Two Sum

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

第一次做力扣,感觉很不舒服用它那个编译器

但是能直接看到许多大佬的解题方法,刷题技巧,空间复杂制度和时间复杂度的排名感觉很厉害,可以用这个进行专题的提升~

我的辣鸡代码C语言

int* twoSum(int* nums, int numsSize, int target, int* returnSize){      int*p=(int*)malloc(sizeof(int)*2);      for(int i=0;i<numsSize-1;i++){          for(int l=i+1;i<numsSize;l++){              if(nums[i]+nums[l]==target){                  p[0]=i;                  p[1]=l;                  *returnSize=2;                  return p;              }          }      }      return p;  }

暴力破解就是这个速度。

后面有加哈希表的提升了不少速度好像。。。

for循环改一改,就优化了一半?

画一个示意图大概就是这样,下面的速度比上面的慢一倍。。。

可能是概率的问题?