int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{  
  int* ii_index = malloc(sizeof(int)*2);
  *returnSize = 2;
    
    for(int i=0;i<numsSize-1;i++)
    {
        for(int j=i+1;j<numsSize; j++)
        {
           if( *(nums+i) + *(nums+j) == target )
           {
               ii_index[0]=i;
               ii_index[1]=j;
               break;
           }
        }
    }

    return ii_index;
}

integer 배열을 받아 배열중에서 더해서 특정값이 되는 랜덤한 2개의 수의 인덱스를 찾아 반환하는 함수.(먼저 찾는 조합을 우선 반환)

리턴값에서 integer pointer를 반환하여야 하므로, 함수 내부에서 malloc을 사용했다. free는 caller 가 호출해줄 것이다. 깜빡하고 빼먹지 않는다면.

 

깃허브 링크:

https://github.com/binary-river/algorithms/blob/Master/twoSum.c

 

GitHub - binary-river/algorithms: algorithms practices

algorithms practices. Contribute to binary-river/algorithms development by creating an account on GitHub.

github.com

 

+ Recent posts