码迷,mamicode.com
首页 > 其他好文 > 详细

Two Sum

时间:2018-01-06 23:23:26      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:you   size   style   tar   input   logs   pre   blog   color   

凯鲁嘎吉 - 博客园

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

 

Two Sum

标签:you   size   style   tar   input   logs   pre   blog   color   

原文地址:https://www.cnblogs.com/kailugaji/p/8215432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!