From 1e7ff624a131a13ae6ac35dbff1810dbfe0d0606 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 17 Sep 2022 23:17:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20rust=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 5ed3ac56..4b17bec6 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -270,6 +270,26 @@ func searchInsert(nums []int, target int) int { } ``` +### Rust + +```rust +impl Solution { + pub fn search_insert(nums: Vec, target: i32) -> i32 { + let mut left = 0; + let mut right = nums.len(); + while left < right { + let mid = (left + right) / 2; + match nums[mid].cmp(&target) { + Ordering::Less => left = mid + 1, + Ordering::Equal => return ((left + right) / 2) as i32, + Ordering::Greater => right = mid, + } + } + ((left + right) / 2) as i32 + } +} +``` + ### Python ```python class Solution: