LeetCode1:Two Sum

https://leetcode.com/problems/two-sum/description/

思路:暴力,对于每个数num[i],查找target-num[i],找到则返回i,j。

代码:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    int i=0,j=0;
    int *res=NULL;
    res=malloc(2*sizeof(int));
    res[0]=res[1]=0;
    for(i=0;i<numsSize;i++){
        for(j=i+1;j<numsSize;j++){
            if(nums[i]+nums[j]==target){
                res[0]=i;
                res[1]=j;
            }
        }
    }
    return res;
}
View Code

 

posted on 2017-10-22 13:55  刘怡凡  阅读(79)  评论(0)    收藏  举报

导航