class Solution { public : int findDuplicate ( vector int nums ) { int Left = 1 , Right = nums . size () - 1 ; while ( Left Right ) { int mid = Left + ( Right - Left ) / 2 ; int cnt = 0 ; //nums数组中小于等于mid的数量 for ( int tmp
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int Left=1,Right=nums.size()-1;
while(Left<Right)
{
int mid=Left+(Right-Left)/2;
int cnt=0;//nums数组中小于等于mid的数量
for(int tmp:nums)
if(tmp<=mid)
cnt++;
if(cnt>mid)Right=mid;
else Left=mid+1;
}
return Left;
}
};