File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 134
134
| 965| [ 单值二叉树] ( https://leetcode.cn/problems/univalued-binary-tree/ ) | [ JavaScript] ( ./algorithms/univalued-binary-tree.js ) | Easy|
135
135
| 977| [ 有序数组的平方] ( https://leetcode.cn/problems/squares-of-a-sorted-array/ ) | [ JavaScript] ( ./algorithms/squares-of-a-sorted-array.js ) | Easy|
136
136
| 988| [ 从叶结点开始的最小字符串] ( https://leetcode.cn/problems/smallest-string-starting-from-leaf/ ) | [ JavaScript] ( ./algorithms/smallest-string-starting-from-leaf.js ) | Medium|
137
+ | 1002| [ 查找共用字符] ( https://leetcode.cn/problems/find-common-characters/ ) | [ JavaScript] ( ./algorithms/find-common-characters.js ) | Easy|
137
138
| 面试题 04.12| [ 面试题 04.12. 求和路径] ( https://leetcode.cn/problems/paths-with-sum-lcci/ ) | [ JavaScript] ( ./algorithms/paths-with-sum-lcci.js ) | Medium|
138
139
| 面试题 02.07| [ 面试题 02.07. 链表相交] ( https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-linked-lists-lcci.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } words
3
+ * @return {string[] }
4
+ */
5
+ var commonChars = function ( words ) {
6
+ // 数组哈希计数
7
+ const result = [ ] ;
8
+ const size = 26 ;
9
+ const firstHash = Array ( size ) . fill ( 0 ) ;
10
+ const firstWord = words [ 0 ] ;
11
+
12
+ for ( let i = 0 ; i < firstWord . length ; i ++ ) {
13
+ const index = firstWord [ i ] . charCodeAt ( ) - "a" . charCodeAt ( ) ;
14
+ firstHash [ index ] ++ ;
15
+ }
16
+ const tempHash = Array ( 26 ) . fill ( 0 ) ;
17
+ for ( let i = 1 ; i < words . length ; i ++ ) {
18
+ const curr = words [ i ] ;
19
+ for ( let j = 0 ; j < curr . length ; j ++ ) {
20
+ const index = curr [ j ] . charCodeAt ( ) - "a" . charCodeAt ( ) ;
21
+ tempHash [ index ] ++ ;
22
+ }
23
+ for ( let k = 0 ; k < size ; k ++ ) {
24
+ firstHash [ k ] = Math . min ( firstHash [ k ] , tempHash [ k ] ) ;
25
+ }
26
+ // reset
27
+ tempHash . fill ( 0 ) ;
28
+ }
29
+
30
+ for ( let i = 0 ; i < firstHash . length ; i ++ ) {
31
+ while ( firstHash [ i ] -- ) {
32
+ result . push ( String . fromCharCode ( i + "a" . charCodeAt ( ) ) ) ;
33
+ }
34
+ }
35
+
36
+ return result ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments