From 78db04b4b70d8cf879b2c7c9f2954f3465500ad0 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 18 Oct 2022 00:29:12 +0800 Subject: [PATCH] =?UTF-8?q?update=201002.=20=E6=9F=A5=E6=89=BE=E5=B8=B8?= =?UTF-8?q?=E7=94=A8=E5=AD=97=E7=AC=A6=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1002.查找常用字符.md | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 78eb1989..fd188660 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -451,6 +451,42 @@ object Solution { } } ``` + +Rust: + +```rust +impl Solution { + pub fn common_chars(words: Vec) -> Vec { + 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 + } +} +``` +