ALGORITHM/c&c++ leetcode
[c/LeetCode] Two Sum
josu_shell
2022. 2. 10. 20:43
난 아직.. returnSize의 역할을 이해 못했다.
어짜피 문제에서 exactly one solution 이라고 강조까지 해놨으니, size는 당연히 2인데 굳이 저걸 포인터로 받아오는 이유는 뭐지?
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *result =(int *)malloc(sizeof(int) * 2);
for(int i=0;i<numsSize;i++)
{
for(int j=i+1;j<numsSize;j++)
{
if(nums[i] + nums[j] == target)
{
result[0] =i;
result[1] =j;
break;
}
}
}
*returnSize = 2;
return result;
}
일단 풀긴 했지만 아직도 시간복잡도가 O(n^2)이다.
더 줄일 수 있는 아이디어는 있는데 테스트 몇번 해봐야할듯