20240715 Finished

This commit is contained in:
jackfiled 2024-07-15 14:02:13 +08:00
parent dd7dbc9d53
commit 63f0d092b9
2 changed files with 44 additions and 1 deletions

View File

@ -174,3 +174,4 @@ mod p79_word_search;
mod p108_convert_sorted_array_to_binary_search_tree;
mod p53_maximum_subarray;
mod p918_maximum_sum_circular_subarray;
mod p35_search_insert_position;

View File

@ -0,0 +1,42 @@
/**
* [35] Search Insert Position
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
let mut l = 0;
let mut r = nums.len() - 1;
let mut result = nums.len();
while l <= r {
let middle = (r - l) / 2 + l;
if target <= nums[middle] {
result = middle;
if middle == 0 {
break;
}
r = middle - 1;
} else {
l = middle + 1;
}
}
result as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_35() {
}
}