20240715 Finished
This commit is contained in:
parent
dd7dbc9d53
commit
63f0d092b9
|
@ -173,4 +173,5 @@ mod p22_generate_parentheses;
|
|||
mod p79_word_search;
|
||||
mod p108_convert_sorted_array_to_binary_search_tree;
|
||||
mod p53_maximum_subarray;
|
||||
mod p918_maximum_sum_circular_subarray;
|
||||
mod p918_maximum_sum_circular_subarray;
|
||||
mod p35_search_insert_position;
|
42
src/problem/p35_search_insert_position.rs
Normal file
42
src/problem/p35_search_insert_position.rs
Normal 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() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user