Two Sum(C)

来源:互联网 发布:21天学通c语言视频 编辑:程序博客网 时间:2024/06/05 14:38

Description: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.


solution:

int* twoSum(int* nums, int numsSize, int target) {
   int i,j;
int *p;  //初始化指针
for(i=0;i<numsSize;i++){
    for(j=i+1;j<numsSize;j++){
        if(nums[i]+nums[j]==target){
        p=(int*)malloc(2*sizeof(int));
        p[0]=i;
        p[1]=j;
        }
        else{
         continue;
        }
    }
}
return p;
}

0 0
原创粉丝点击