File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 9
9
| 14| [ 最长公共前缀] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript] ( ./algorithms/longest-common-prefix.js ) | Easy|
10
10
| 21| [ 合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ ) | [ JavaScript] ( ./algorithms/merge-two-sorted-lists.js ) | Easy|
11
11
| 189| [ 轮转数组] ( https://leetcode-cn.com/problems/rotate-array/ ) | [ JavaScript] ( ./algorithms/rotate-array.js ) | Medium|
12
- | 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
12
+ | 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
13
+ | 350| [ 两个数组的交集 II] ( https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays-ii.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 映射
3
+ * @param {number[] } nums1
4
+ * @param {number[] } nums2
5
+ * @return {number[] }
6
+ */
7
+ var intersect = function ( nums1 , nums2 ) {
8
+ // 保存 nums2 中每个数字出现的次数
9
+ const o = { }
10
+ const result = [ ]
11
+
12
+ for ( let i = 0 ; i < nums2 . length ; i ++ ) {
13
+ const curr = nums2 [ i ]
14
+ o [ curr ] || ( o [ curr ] = 0 )
15
+ o [ curr ] ++
16
+ }
17
+
18
+ for ( let i = 0 ; i < nums1 . length ; i ++ ) {
19
+ const curr = nums1 [ i ]
20
+
21
+ if ( o [ curr ] ) {
22
+ result . push ( curr )
23
+ o [ curr ] --
24
+ }
25
+ }
26
+
27
+ return result
28
+ } ;
29
+
30
+ // 双指针
31
+ // ...
You can’t perform that action at this time.
0 commit comments