版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://cloud.tencent.com/developer/article/1535071
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].
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循环改一改,就优化了一半?