diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5aff6ac..7a51020 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; \ No newline at end of file +mod p918_maximum_sum_circular_subarray; +mod p35_search_insert_position; \ No newline at end of file diff --git a/src/problem/p35_search_insert_position.rs b/src/problem/p35_search_insert_position.rs new file mode 100644 index 0000000..e1c007a --- /dev/null +++ b/src/problem/p35_search_insert_position.rs @@ -0,0 +1,42 @@ +/** + * [35] Search Insert Position + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn search_insert(nums: Vec, 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() { + } +}