Skip to content

Commit a766a7e

Browse files
authored
Update Solution.rs
1 parent 1355e3b commit a766a7e

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
use std::cmp::Ordering;
2-
31
impl Solution {
42
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
5-
let mut l = 0;
6-
let mut r = nums.len();
3+
let mut l: usize = 0;
4+
let mut r: usize = nums.len() - 1;
75
while l < r {
86
let mid = (l + r) >> 1;
9-
match nums[mid].cmp(&target) {
10-
Ordering::Less => {
11-
l = mid + 1;
12-
}
13-
Ordering::Greater => {
14-
r = mid;
15-
}
16-
Ordering::Equal => {
17-
return mid as i32;
18-
}
7+
if nums[mid] >= target {
8+
r = mid;
9+
} else {
10+
l = mid + 1;
1911
}
2012
}
21-
-1
13+
if nums[l] == target {
14+
l as i32
15+
} else {
16+
-1
17+
}
2218
}
2319
}

0 commit comments

Comments
 (0)