Leetcode刷题笔记1:Two Sum(C语言)

Leetcode刷题笔记1:Two Sum(C语言)_第1张图片

 

 

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) 
{
    int i,j,flag=0;
    int *s = (int*)malloc(2*sizeof(int));
    for(i=0;i

得到以下结果:

感觉太慢了,又改了改:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    static int s[2] = {0};
    for(int i = 0; i < numsSize - 1;i++)
    {
        for(int j = i+1;j < numsSize;j++)
        {
            if(target == (nums[i]+nums[j]))
            {
                a[0] = i;a[1] = j; return s;
            }
        }
    }
    return 0;
}
时间上有好一点点:

 

 

你可能感兴趣的:(数据结构/算法/刷题)