LeetCode刷题-001两数之和

给定一个整数数列,找出其中和为特定值的那两个数。
你可以假设每个输入都只会有一种答案,同样的元素不能被重用。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

 1 int* twoSum(int* nums, int numsSize, int target) 
 2 {
 3     int i,j;
 4     int* p=(int*)malloc(sizeof(int)*2);
 5     for(i=0;i<numsSize;i++)
 6     {
 7         for(j=i+1;j<numsSize;j++)
 8         {
 9             if(nums[i]+nums[j]==target)
10             {
11                 p[0]=i;
12                 p[1]=j;
13             }
14         }
15     }
16     return p;
17 }

猜你喜欢

转载自www.cnblogs.com/nkqlhqc/p/9085295.html