Easy understood 4-line C solution


  • 3
    W

    a general method

    int findDuplicate(int *nums, int numsSize)
    {
      int s = nums[0], f = nums[nums[0]];
      for (; s != f; s = nums[s], f = nums[nums[f]]) ;
      for (f = 0; s != f; s = nums[s], f = nums[f]) ;
      return s;
    }
    

Log in to reply
 

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