beats 93%,3ms,in C


  • 0
    K

    '''
    int cmp(const void *a,const void *b)
    {

    return *(int*)a - *(int*)b;
    

    }
    int* twoSum(int* nums, int numsSize, int target)
    {

    int i,j;
    int *ret;
    int *temp;
    ret = (int*)malloc(2*sizeof(int));
    temp = (int*)malloc(numsSize*2*sizeof(int));
    for(i=0;i<numsSize;i++)
    {
    	temp[2*i] = nums[i];
    	temp[2*i+1] = i; 
    }
    qsort(temp,numsSize,2*sizeof(temp[0]),cmp);
    i = 0;
    j = numsSize - 1;
    while(temp[2*i]+temp[2*j] != target)
    {
        if(temp[2*i]+temp[2*j] > target)
        {
            --j;
        }
        else if(temp[2*i]+temp[2*j] < target)
        {
            ++i;
        }
        else
        {
            break;
        }
    }
    ret[0] = temp[2*i+1] < temp[2*j+1] ? temp[2*i+1]: temp[2*j+1];
    ret[1] = temp[2*i+1] > temp[2*j+1] ? temp[2*i+1]: temp[2*j+1];
    return ret;
    

    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.