mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 1002. 查找常用字符 about rust
This commit is contained in:
@ -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"/>
|
||||
|
Reference in New Issue
Block a user