面试题 03
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3Solutions
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
for (int i = 0; i < nums.size(); i++)
// find the right one for this position
while (i != nums[i]) {
// find duplicate
if (nums[nums[i]] == nums[i])
return nums[i];
// put the number in this position into the right position
swap(nums[i], nums[nums[i]]);
}
return 0;
}
};Last updated