From b55d593cb726630f3800b16256161a53a554ebcc Mon Sep 17 00:00:00 2001 From: nanhuaibeian <49868746+nanhuaibeian@users.noreply.github.com> Date: Wed, 12 May 2021 20:57:14 +0800 Subject: [PATCH] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index ca9b6061..742bfdc3 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -111,7 +111,31 @@ public: Java: +```Java +class Solution { + public boolean canConstruct(String ransomNote, String magazine) { + //记录杂志字符串出现的次数 + int[] arr = new int[26]; + int temp; + for (int i = 0; i < magazine.length(); i++) { + temp = magazine.charAt(i) - 'a'; + arr[temp]++; + } + for (int i = 0; i < ransomNote.length(); i++) { + temp = ransomNote.charAt(i) - 'a'; + //对于金信中的每一个字符都在数组中查找 + //找到相应位减一,否则找不到返回false + if (arr[temp] > 0) { + arr[temp]--; + } else { + return false; + } + } + return true; + } +} +``` Python: