File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
algorithms/easy/intersect Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ LeetCode Problems' Solutions in JavaScript
13137 . [ 从排序数组中删除重复项] ( ./algorithms/easy/removeDuplicates/removeDuplicates.js )
14148 . [ 反转整数] ( ./algorithms/easy/reverse/reverse.js )
15159 . [ 第一个错误的版本] ( ./algorithms/easy/solution/solution.js )
16+ 10 . [ 两个数组的交集 II] ( ./algorithms/easy/intersect/intersect.js )
1617
1718## medium
1819
Original file line number Diff line number Diff line change 1+ // description: https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/26/
2+ // 两个数组的交集 II
3+
4+ /**
5+ * @param {number[] } nums1
6+ * @param {number[] } nums2
7+ * @return {number[] }
8+ */
9+ var intersect = function ( nums1 , nums2 ) {
10+ nums1 = nums1 . sort ( ( a , b ) => a - b ) ;
11+ nums2 = nums2 . sort ( ( a , b ) => a - b ) ;
12+
13+ const ret = [ ] ;
14+
15+ const len1 = nums1 . length ;
16+ const len2 = nums2 . length ;
17+
18+ let k = 0 ;
19+
20+ for ( let i = 0 ; i < len1 ; i ++ ) {
21+ for ( let j = k ; j < len2 ; j ++ ) {
22+ if ( nums1 [ i ] === nums2 [ j ] ) {
23+ k = j + 1 ;
24+ ret . push ( nums1 [ i ] ) ;
25+ break ;
26+ }
27+ }
28+ }
29+
30+ return ret ;
31+ } ;
You can’t perform that action at this time.
0 commit comments