File tree Expand file tree Collapse file tree 1 file changed +6
-10
lines changed Expand file tree Collapse file tree 1 file changed +6
-10
lines changed Original file line number Diff line number Diff line change @@ -224,32 +224,28 @@ javaScript
224
224
var commonChars = function (words ) {
225
225
let res = []
226
226
let size = 26
227
- let firstHash = new Array (size)
228
- for (let i = 0 ; i < size; i++ ) { // 初始化 hash 数组
229
- firstHash[i] = 0
230
- }
227
+ let firstHash = new Array (size).fill (0 ) // 初始化 hash 数组
231
228
232
229
let a = " a" .charCodeAt ()
233
230
let firstWord = words[0 ]
234
231
for (let i = 0 ; i < firstWord .length ; i++ ) { // 第 0 个单词的统计
235
232
let idx = firstWord[i].charCodeAt ()
236
233
firstHash[idx - a] += 1
237
234
}
238
-
235
+
236
+ let otherHash = new Array (size).fill (0 ) // 初始化 hash 数组
239
237
for (let i = 1 ; i < words .length ; i++ ) { // 1-n 个单词统计
240
- let otherHash = new Array (size)
241
- for (let i = 0 ; i < size; i++ ) { // 初始化 hash 数组
242
- otherHash[i] = 0
243
- }
244
-
245
238
for (let j = 0 ; j < words[i].length ; j++ ) {
246
239
let idx = words[i][j].charCodeAt ()
247
240
otherHash[idx - a] += 1
248
241
}
242
+
249
243
for (let i = 0 ; i < size; i++ ) {
250
244
firstHash[i] = Math .min (firstHash[i], otherHash[i])
251
245
}
246
+ otherHash .fill (0 )
252
247
}
248
+
253
249
for (let i = 0 ; i < size; i++ ) {
254
250
while (firstHash[i] > 0 ) {
255
251
res .push (String .fromCharCode (i + a))
You can’t perform that action at this time.
0 commit comments