From 987597926fa9de3f742fa5ac4201e231500775c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=A0=91=E6=A3=AE?= <14021051@buaa.edu.cn> Date: Mon, 8 Oct 2018 23:27:54 +0800 Subject: [PATCH] Create 383. Ransom Note.md --- 383. Ransom Note.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 383. Ransom Note.md diff --git a/383. Ransom Note.md b/383. Ransom Note.md new file mode 100644 index 0000000..8107a15 --- /dev/null +++ b/383. Ransom Note.md @@ -0,0 +1,16 @@ +# [383. Ransom Note](https://leetcode.com/problems/ransom-note/description/) +# 思路 +用一个大小为26的数组count记录magazine中26个字母的出现次数,只要每个字母出现次数不小于ransomNote对应字母次数就行了。 +# C++ +``` +class Solution { +public: + bool canConstruct(string ransomNote, string magazine) { + vectorcount(26); + for(char c: magazine) count[c - 'a']++; + for(char c: ransomNote) + if(0 > --count[c - 'a']) return false; + return true; + } +}; +```