讨论/《二分查找》 - 寻找重复数/
《二分查找》 - 寻找重复数
共 1 个回复

c++
class Solution {
public:
int findDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
int size = nums.size();
int left{0}, right{size-1};
while (left < right){
int mid = left + (right-left)/2;
if (nums[mid] < mid+1) right = mid;
else left = mid + 1;
}
return nums[left];
}
};