Time O(logN) Space O(1)
100.00 %
class Solution {
public int singleNonDuplicate(int[] nums) {
int start = 0;
int end = (nums.length - 1) / 2;
// find the first lead index that nums[index] != nums[index + 1]
while (start < end) {
int mid = start + (end - start) / 2;
if (nums[mid * 2] == nums[mid * 2 + 1]) {
start = mid + 1;
} else {
end = mid;
}
}
return nums[start * 2];
}
}
No comments:
Post a Comment