C code Without modifying the array O(1)space and O(N) time


  • -5
    T

    int findDuplicate(int* nums, int numsSize)
    {
    if (0 == numsSize) {
    return 0;
    }
    int dst = 0;

    for (int i = 0; i < numsSize; i++) {
    	nums[(nums[i] - 1) %  numsSize] += numsSize;
    }
    for (int i = 0; i < numsSize; i++) {
    	if (nums[i] > numsSize* 2) {
    		dst = i + 1;
    		break;
    	}
    }
    for (int i = 0; i < numsSize; i++) {
    	while (nums[i] > numsSize)
    		nums[i] -= numsSize;
    }
    return dst;
    

    }


  • 1
    I

    I think it is incorrect, because you modify the array.


Log in to reply
 

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