Skip to content

Instantly share code, notes, and snippets.

@leearmee35
Created March 2, 2017 22:34
Show Gist options
  • Save leearmee35/864a07de8578e306198cefc82d1f5992 to your computer and use it in GitHub Desktop.
Save leearmee35/864a07de8578e306198cefc82d1f5992 to your computer and use it in GitHub Desktop.
287. Find the Duplicate Number
public class Solution {
public int findDuplicate(int[] nums) {
int low = 1, high = nums.length - 1;
while (low <= high) {
int mid = (int) (low + (high - low) * 0.5);
int cnt = 0;
for (int a : nums) {
if (a <= mid) ++cnt;
}
if (cnt <= mid) low = mid + 1;
else high = mid - 1;
}
return low;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment