思路:看官方解答
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int n=nums.size();
int left=1;
int right=n-1;
int ans=-1;
while(left<=right){
int mid=(left+right)/2;
int count=0;
for(int j=0;j<n;j++){
if(nums[j]<=mid){
count++;
}
}
if(count<=mid){
left=mid+1;
}
else{
right=mid-1;
ans=mid;
}
}
return ans;
}
};