mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1997 from fwqaaq/patch-28
Update 0763.划分字母区间.md 优化 rust
This commit is contained in:
@ -412,28 +412,22 @@ object Solution {
|
|||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
use std::collections::HashMap;
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn max (a: usize, b: usize) -> usize {
|
|
||||||
if a > b { a } else { b }
|
|
||||||
}
|
|
||||||
pub fn partition_labels(s: String) -> Vec<i32> {
|
pub fn partition_labels(s: String) -> Vec<i32> {
|
||||||
let s = s.chars().collect::<Vec<char>>();
|
let mut hash = vec![0; 26];
|
||||||
let mut hash: HashMap<char, usize> = HashMap::new();
|
for (i, &c) in s.as_bytes().iter().enumerate() {
|
||||||
for i in 0..s.len() {
|
hash[(c - b'a') as usize] = i;
|
||||||
hash.insert(s[i], i);
|
|
||||||
}
|
}
|
||||||
let mut result: Vec<i32> = Vec::new();
|
let mut res = vec![];
|
||||||
let mut left: usize = 0;
|
let (mut left, mut right) = (0, 0);
|
||||||
let mut right: usize = 0;
|
for (i, &c) in s.as_bytes().iter().enumerate() {
|
||||||
for i in 0..s.len() {
|
right = right.max(hash[(c - b'a') as usize]);
|
||||||
right = Self::max(right, hash[&s[i]]);
|
|
||||||
if i == right {
|
if i == right {
|
||||||
result.push((right - left + 1) as i32);
|
res.push((right - left + 1) as i32);
|
||||||
left = i + 1;
|
left = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user