From 1840a8aa3502e84e124d5d967ea417562690aa16 Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Sun, 12 Sep 2021 16:53:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A00383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1Rust=E8=AA=9E=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: - leetcode提交通過 --- problems/0383.赎金信.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index d1594015..75b31698 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -315,6 +315,28 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { } ``` +Rust: +```rust +impl Solution { + pub fn can_construct(ransom_note: String, magazine: String) -> bool { + let baseChar = 'a'; + let mut record = vec![0; 26]; + + for byte in magazine.bytes() { + record[byte as usize - baseChar as usize] += 1; + } + + for byte in ransom_note.bytes() { + record[byte as usize - baseChar as usize] -= 1; + if record[byte as usize - baseChar as usize] < 0 { + return false; + } + } + + return true; + } +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)