Merge pull request #1560 from cezarbbb/Greedy08

添加 0763.划分字母区间 Rust版本
This commit is contained in:
程序员Carl
2022-08-17 10:00:00 +08:00
committed by GitHub

View File

@ -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<i32> {
let s = s.chars().collect::<Vec<char>>();
let mut hash: HashMap<char, usize> = HashMap::new();
for i in 0..s.len() {
hash.insert(s[i], i);
}
let mut result: Vec<i32> = 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
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>