What's wrong with the my code, two sum, N^2 solution

  • -1

    The code below kept complained time exceeds error message. Anyone has idea? Thanks

    int* twoSum(int* nums, int numsSize, int target) {
        if (numsSize<2)
           return NULL;
        int *out = (int*)malloc(sizeof(int)*2);
        for (int i=0; i<numsSize-1; i++){
            for (int j=i+1; j<numsSize; j++) {
                if (nums[i]+nums[j]==target) {
                    return out;
        return NULL;

  • 0

    The time complexity of your code is O(n^2). It's not a good idea if the input is too long. This problem requires an expected performance of the solution.

Log in to reply

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