From 2208d96581e13b19e4d41610f259b04ad3a75856 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 31 Mar 2023 18:37:51 +0800 Subject: [PATCH] =?UTF-8?q?Update=200763.=E5=88=92=E5=88=86=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0763.划分字母区间.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index ad680ef9..fbcafdc8 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -412,28 +412,22 @@ 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 hash = vec![0; 26]; + for (i, &c) in s.as_bytes().iter().enumerate() { + hash[(c - b'a') as usize] = 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]]); + let mut res = vec![]; + let (mut left, mut right) = (0, 0); + for (i, &c) in s.as_bytes().iter().enumerate() { + right = right.max(hash[(c - b'a') as usize]); if i == right { - result.push((right - left + 1) as i32); + res.push((right - left + 1) as i32); left = i + 1; } } - result + res } } ```