update 1002. 查找常用字符 about rust

This commit is contained in:
fw_qaq
2022-10-18 00:29:12 +08:00
committed by GitHub
parent f3f6cab312
commit 78db04b4b7

View File

@ -451,6 +451,42 @@ object Solution {
}
}
```
Rust:
```rust
impl Solution {
pub fn common_chars(words: Vec<String>) -> Vec<String> {
if words.is_empty() {
return vec![];
}
let mut res = vec![];
let mut hash = vec![0; 26];
for i in words[0].bytes() {
hash[(i - b'a') as usize] += 1;
}
for i in words.iter().skip(1) {
let mut other_hash_str = vec![0; 26];
for j in i.bytes() {
other_hash_str[(j - b'a') as usize] += 1;
}
for k in 0..26 {
hash[k] = hash[k].min(other_hash_str[k]);
}
}
for (i, v) in hash.iter_mut().enumerate() {
while *v > 0 {
res.push(((i as u8 + b'a') as char).to_string());
*v -= 1;
}
}
res
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>