Skip to content

Commit e74be85

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 540_Single_Element_in_a_Sorted_Array.java
1 parent 0ea9827 commit e74be85

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
class Solution {
22
public int singleNonDuplicate(int[] nums) {
3-
if (nums == null || nums.length == 0) {
4-
return 0;
5-
}
3+
int low = 0, high = nums.length - 1;
64

7-
int start = 0, end = nums.length - 1;
5+
while (low < high) {
6+
int mid = low + (high - low) / 2;
87

9-
while (start < end) {
10-
int mid = start + (end - start) / 2;
8+
if (mid % 2 == 1) {
9+
mid--;
10+
}
1111

12-
if (mid % 2 == 0) {
13-
if (nums[mid] == nums[mid + 1]) {
14-
start = mid + 1;
15-
} else {
16-
end = mid;
17-
}
12+
if (nums[mid] != nums[mid + 1]) {
13+
high = mid;
1814
} else {
19-
if (nums[mid] == nums[mid - 1]) {
20-
start = mid + 1;
21-
} else {
22-
end = mid;
23-
}
15+
low = mid + 2;
2416
}
2517
}
2618

27-
return nums[start];
19+
return nums[low];
2820
}
2921
}

0 commit comments

Comments
 (0)