Skip to content

Commit c09d1d3

Browse files
committed
feat: add two validations in search insert
1 parent 07c7190 commit c09d1d3

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/easy/search_insert_position.rs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
55
let mut right = nums.len() - 1;
66
let mut mid = 0;
77

8+
if target < nums[0] {
9+
return 0;
10+
} else if target > nums[nums.len() - 1] {
11+
return nums.len() as i32;
12+
}
13+
814
while left <= right {
915
mid = (left + right) / 2;
1016
if nums[mid] == target {

0 commit comments

Comments
 (0)