classSolution { public: intduplicateInArray(vector<int>& nums){ int n = nums.size(); for (int i = 0; i < n; ++i) if (nums[i] < 0 || nums[i] > n - 1) return-1; for (int i = 0; i < n; ++i) { while (i != nums[i]) { int x = nums[i]; if (nums[x] == x) return x; //如果坑位已经被占了,说明重复了。 else swap(nums[x], nums[i]); } } x return-1; } };