参考环形链表Ⅱ,将数组建立类似的映射关系
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int slow = 0, fast = 0;
slow = nums[slow];
fast = nums[nums[fast]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
int ans = 0;
while(ans != slow){
ans = nums[ans];
slow = nums[slow];
}
return ans;
}
};