From d92d7207cb8ec4d8628c41e8aaeca6145ca76823 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sun, 17 Oct 2021 10:20:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=201002=20=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E5=AD=97=E7=AC=A6=20JavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对两个数组的初值写法进行简化 --- problems/1002.查找常用字符.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 09e49c4f..44a02ceb 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -224,10 +224,7 @@ javaScript var commonChars = function (words) { let res = [] let size = 26 - let firstHash = new Array(size) - for (let i = 0; i < size; i++) { // 初始化 hash 数组 - firstHash[i] = 0 - } + let firstHash = new Array(size).fill(0) // 初始化 hash 数组 let a = "a".charCodeAt() let firstWord = words[0] @@ -235,21 +232,20 @@ var commonChars = function (words) { let idx = firstWord[i].charCodeAt() firstHash[idx - a] += 1 } - + + let otherHash = new Array(size).fill(0) // 初始化 hash 数组 for (let i = 1; i < words.length; i++) { // 1-n 个单词统计 - let otherHash = new Array(size) - for (let i = 0; i < size; i++) { // 初始化 hash 数组 - otherHash[i] = 0 - } - for (let j = 0; j < words[i].length; j++) { let idx = words[i][j].charCodeAt() otherHash[idx - a] += 1 } + for (let i = 0; i < size; i++) { firstHash[i] = Math.min(firstHash[i], otherHash[i]) } + otherHash.fill(0) } + for (let i = 0; i < size; i++) { while (firstHash[i] > 0) { res.push(String.fromCharCode(i + a))