0287.cpp (448B)
1 class Solution { 2 public: 3 int findDuplicate(vector<int> &nums) { 4 int slow = 0, fast = 0; 5 while (true) { 6 fast = nums[nums[fast]]; 7 slow = nums[slow]; 8 if (fast == slow) { 9 fast = 0; 10 while (fast != slow) { 11 fast = nums[fast]; 12 slow = nums[slow]; 13 } 14 return fast; 15 } 16 } 17 } 18 };