Created
March 2, 2017 22:34
-
-
Save leearmee35/2b6a01d71752d0619209550a448402fc to your computer and use it in GitHub Desktop.
287. Find the Duplicate Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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