코딩테스트/Leetcode

Leetcode - [Easy]1. Two Sum

aiemag 2021. 3. 4. 23:07
반응형

leetcode.com/problems/two-sum/

 

Two Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
 
    *returnSize = 2;
    int* rstAry = (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) {
                rstAry[0= i;
                rstAry[1= j;
 
                return rstAry;
            }
        }
    }
 
    return rstAry;
}
cs

 

반응형