Iam_Sandeep

287. Find the Duplicate Number

Jul 29th, 2022
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. class Solution:
  2.     def findDuplicate(self, nums: List[int]) -> int:
  3.         n=len(nums)
  4.         s,f=nums[0],nums[nums[0]]
  5.         while s!=f:
  6.             s=nums[s]
  7.             f=nums[nums[f]]
  8.         #now make reset f to 0 and both s,f move only step. To meet the duplicate number
  9.         f=0
  10.         while s!=f:
  11.             s,f=nums[s],nums[f]
  12.         return s
  13.        
Advertisement
Add Comment
Please, Sign In to add comment