From 983bb606d39c8ea5ab384d15d8c1bb226b65bce9 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 24 Nov 2022 21:51:48 +0800 Subject: [PATCH] =?UTF-8?q?update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1:=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 57458308..4e6ba033 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -147,11 +147,11 @@ class Solution: arr = [0] * 26 - for x in magazine: + for x in magazine: # 记录 magazine里各个字符出现次数 arr[ord(x) - ord('a')] += 1 - for x in ransomNote: - if arr[ord(x) - ord('a')] == 0: + for x in ransomNote: # 在arr里对应的字符个数做--操作 + if arr[ord(x) - ord('a')] == 0: # 如果没有出现过直接返回 return False else: arr[ord(x) - ord('a')] -= 1 @@ -234,12 +234,12 @@ Go: ```go func canConstruct(ransomNote string, magazine string) bool { record := make([]int, 26) - for _, v := range magazine { + for _, v := range magazine { // 通过recode数据记录 magazine里各个字符出现次数 record[v-'a']++ } - for _, v := range ransomNote { + for _, v := range ransomNote { // 遍历ransomNote,在record里对应的字符个数做--操作 record[v-'a']-- - if record[v-'a'] < 0 { + if record[v-'a'] < 0 { // 如果小于零说明ransomNote里出现的字符,magazine没有 return false } } @@ -258,12 +258,12 @@ javaScript: var canConstruct = function(ransomNote, magazine) { const strArr = new Array(26).fill(0), base = "a".charCodeAt(); - for(const s of magazine) { + for(const s of magazine) { // 记录 magazine里各个字符出现次数 strArr[s.charCodeAt() - base]++; } - for(const s of ransomNote) { + for(const s of ransomNote) { // 对应的字符个数做--操作 const index = s.charCodeAt() - base; - if(!strArr[index]) return false; + if(!strArr[index]) return false; // 如果没记录过直接返回false strArr[index]--; } return true;