From e5dd7c3ea53c7498503ed39c7ffa7ab9e812ede9 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200763.=E5=88=92=E5=88=86?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0763.划分字母区间 Rust版本 --- problems/0763.划分字母区间.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index ecf064d3..a6066ebc 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -343,6 +343,34 @@ object Solution { } ``` +### Rust + +```Rust +use std::collections::HashMap; +impl Solution { + fn max (a: usize, b: usize) -> usize { + if a > b { a } else { b } + } + pub fn partition_labels(s: String) -> Vec { + let s = s.chars().collect::>(); + let mut hash: HashMap = HashMap::new(); + for i in 0..s.len() { + hash.insert(s[i], i); + } + let mut result: Vec = Vec::new(); + let mut left: usize = 0; + let mut right: usize = 0; + for i in 0..s.len() { + right = Self::max(right, hash[&s[i]]); + if i == right { + result.push((right - left + 1) as i32); + left = i + 1; + } + } + result + } +} +``` -----------------------