欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Two Sum

程序员文章站 2022-03-26 12:28:22
凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ Question: Answer: ......

凯鲁嘎吉 - 博客园

http://www.cnblogs.com/kailugaji/

Question:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].

Answer:

int* twoSum(int* nums, int numsSize, int target) {
int j, i;
int *test = NULL;
test = (int )malloc(2*sizeof(int));
 for (i=0; i<numsSize ;i++){
  for (j=i+1; j<numsSize ;j++){
   if(nums[i]+nums[j] == target){
    *(test) = i;
    *(test+1) = j;
    }
  }
 }
 return test;
}
Run Code Result:
Your input
[3,2,4]
6
Your answer
[1,2]
Expected answer
[1,2]
Show Diff
Runtime: 4 ms